Skip to content

Commit

Permalink
Auto merge of rust-lang#100195 - matthiaskrgr:rollup-ovzyyb0, r=matth…
Browse files Browse the repository at this point in the history
…iaskrgr

Rollup of 4 pull requests

Successful merges:

 - rust-lang#100094 (Detect type mismatch due to loop that might never iterate)
 - rust-lang#100132 (Use (actually) dummy place for let-else divergence)
 - rust-lang#100167 (Recover `require`, `include` instead of `use` in item)
 - rust-lang#100193 (Remove more Clean trait implementations)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Aug 6, 2022
2 parents 76b0484 + 811b036 commit 5c54be3
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 53 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/build/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2334,7 +2334,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// This place is not really used because this destination place
// should never be used to take values at the end of the failure
// block.
let dummy_place = Place { local: RETURN_PLACE, projection: ty::List::empty() };
let dummy_place = self.temp(self.tcx.types.never, else_block.span);
let failure_block;
unpack!(
failure_block = self.ast_block(
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,10 @@ impl<'a> Parser<'a> {
// MACRO_RULES ITEM
self.parse_item_macro_rules(vis, has_bang)?
} else if self.isnt_macro_invocation()
&& (self.token.is_ident_named(sym::import) || self.token.is_ident_named(sym::using))
&& (self.token.is_ident_named(sym::import)
|| self.token.is_ident_named(sym::using)
|| self.token.is_ident_named(sym::include)
|| self.token.is_ident_named(sym::require))
{
return self.recover_import_as_use();
} else if self.isnt_macro_invocation() && vis.kind.is_pub() {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,7 @@ symbols! {
repr_packed,
repr_simd,
repr_transparent,
require,
residual,
result,
rhs,
Expand Down
49 changes: 48 additions & 1 deletion compiler/rustc_typeck/src/check/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@
use crate::astconv::AstConv;
use crate::check::FnCtxt;
use rustc_errors::{
struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed,
struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan,
};
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::Expr;
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use rustc_infer::infer::{Coercion, InferOk, InferResult};
use rustc_infer::traits::{Obligation, TraitEngine, TraitEngineExt};
Expand Down Expand Up @@ -87,6 +89,19 @@ impl<'a, 'tcx> Deref for Coerce<'a, 'tcx> {

type CoerceResult<'tcx> = InferResult<'tcx, (Vec<Adjustment<'tcx>>, Ty<'tcx>)>;

struct CollectRetsVisitor<'tcx> {
ret_exprs: Vec<&'tcx hir::Expr<'tcx>>,
}

impl<'tcx> Visitor<'tcx> for CollectRetsVisitor<'tcx> {
fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
if let hir::ExprKind::Ret(_) = expr.kind {
self.ret_exprs.push(expr);
}
intravisit::walk_expr(self, expr);
}
}

/// Coercing a mutable reference to an immutable works, while
/// coercing `&T` to `&mut T` should be forbidden.
fn coerce_mutbls<'tcx>(
Expand Down Expand Up @@ -1481,6 +1496,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {

let mut err;
let mut unsized_return = false;
let mut visitor = CollectRetsVisitor { ret_exprs: vec![] };
match *cause.code() {
ObligationCauseCode::ReturnNoExpression => {
err = struct_span_err!(
Expand All @@ -1506,6 +1522,10 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
if !fcx.tcx.features().unsized_locals {
unsized_return = self.is_return_ty_unsized(fcx, blk_id);
}
if let Some(expression) = expression
&& let hir::ExprKind::Loop(loop_blk, ..) = expression.kind {
intravisit::walk_block(& mut visitor, loop_blk);
}
}
ObligationCauseCode::ReturnValue(id) => {
err = self.report_return_mismatched_types(
Expand Down Expand Up @@ -1551,12 +1571,39 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
);
}

if visitor.ret_exprs.len() > 0 && let Some(expr) = expression {
self.note_unreachable_loop_return(&mut err, &expr, &visitor.ret_exprs);
}
err.emit_unless(unsized_return);

self.final_ty = Some(fcx.tcx.ty_error());
}
}
}
fn note_unreachable_loop_return<'a>(
&self,
err: &mut DiagnosticBuilder<'a, ErrorGuaranteed>,
expr: &hir::Expr<'tcx>,
ret_exprs: &Vec<&'tcx hir::Expr<'tcx>>,
) {
let hir::ExprKind::Loop(_, _, _, loop_span) = expr.kind else { return;};
let mut span: MultiSpan = vec![loop_span].into();
span.push_span_label(loop_span, "this might have zero elements to iterate on".to_string());
for ret_expr in ret_exprs {
span.push_span_label(
ret_expr.span,
"if the loop doesn't execute, this value would never get returned".to_string(),
);
}
err.span_note(
span,
"the function expects a value to always be returned, but loops might run zero times",
);
err.help(
"return a value for the case when the loop has zero elements to iterate on, or \
consider changing the return type to account for that possibility",
);
}

fn report_return_mismatched_types<'a>(
&self,
Expand Down
90 changes: 42 additions & 48 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,40 +126,40 @@ impl<'tcx> Clean<'tcx, Item> for DocModule<'tcx> {
}
}

impl<'tcx> Clean<'tcx, Option<GenericBound>> for hir::GenericBound<'tcx> {
fn clean(&self, cx: &mut DocContext<'tcx>) -> Option<GenericBound> {
Some(match *self {
hir::GenericBound::Outlives(lt) => GenericBound::Outlives(clean_lifetime(lt, cx)),
hir::GenericBound::LangItemTrait(lang_item, span, _, generic_args) => {
let def_id = cx.tcx.require_lang_item(lang_item, Some(span));

let trait_ref = ty::TraitRef::identity(cx.tcx, def_id).skip_binder();

let generic_args = generic_args.clean(cx);
let GenericArgs::AngleBracketed { bindings, .. } = generic_args
else {
bug!("clean: parenthesized `GenericBound::LangItemTrait`");
};
fn clean_generic_bound<'tcx>(
bound: &hir::GenericBound<'tcx>,
cx: &mut DocContext<'tcx>,
) -> Option<GenericBound> {
Some(match *bound {
hir::GenericBound::Outlives(lt) => GenericBound::Outlives(clean_lifetime(lt, cx)),
hir::GenericBound::LangItemTrait(lang_item, span, _, generic_args) => {
let def_id = cx.tcx.require_lang_item(lang_item, Some(span));

let trait_ref = ty::TraitRef::identity(cx.tcx, def_id).skip_binder();

let generic_args = generic_args.clean(cx);
let GenericArgs::AngleBracketed { bindings, .. } = generic_args
else {
bug!("clean: parenthesized `GenericBound::LangItemTrait`");
};

let trait_ = clean_trait_ref_with_bindings(cx, trait_ref, &bindings);
GenericBound::TraitBound(
PolyTrait { trait_, generic_params: vec![] },
hir::TraitBoundModifier::None,
)
let trait_ = clean_trait_ref_with_bindings(cx, trait_ref, &bindings);
GenericBound::TraitBound(
PolyTrait { trait_, generic_params: vec![] },
hir::TraitBoundModifier::None,
)
}
hir::GenericBound::Trait(ref t, modifier) => {
// `T: ~const Destruct` is hidden because `T: Destruct` is a no-op.
if modifier == hir::TraitBoundModifier::MaybeConst
&& cx.tcx.lang_items().destruct_trait() == Some(t.trait_ref.trait_def_id().unwrap())
{
return None;
}
hir::GenericBound::Trait(ref t, modifier) => {
// `T: ~const Destruct` is hidden because `T: Destruct` is a no-op.
if modifier == hir::TraitBoundModifier::MaybeConst
&& cx.tcx.lang_items().destruct_trait()
== Some(t.trait_ref.trait_def_id().unwrap())
{
return None;
}

GenericBound::TraitBound(clean_poly_trait_ref(t, cx), modifier)
}
})
}
GenericBound::TraitBound(clean_poly_trait_ref(t, cx), modifier)
}
})
}

pub(crate) fn clean_trait_ref_with_bindings<'tcx>(
Expand Down Expand Up @@ -207,12 +207,6 @@ fn clean_poly_trait_ref_with_bindings<'tcx>(
)
}

impl<'tcx> Clean<'tcx, GenericBound> for ty::PolyTraitRef<'tcx> {
fn clean(&self, cx: &mut DocContext<'tcx>) -> GenericBound {
clean_poly_trait_ref_with_bindings(cx, *self, &[])
}
}

fn clean_lifetime<'tcx>(lifetime: hir::Lifetime, cx: &mut DocContext<'tcx>) -> Lifetime {
let def = cx.tcx.named_region(lifetime.hir_id);
if let Some(
Expand Down Expand Up @@ -294,14 +288,14 @@ impl<'tcx> Clean<'tcx, Option<WherePredicate>> for hir::WherePredicate<'tcx> {
.collect();
WherePredicate::BoundPredicate {
ty: clean_ty(wbp.bounded_ty, cx),
bounds: wbp.bounds.iter().filter_map(|x| x.clean(cx)).collect(),
bounds: wbp.bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(),
bound_params,
}
}

hir::WherePredicate::RegionPredicate(ref wrp) => WherePredicate::RegionPredicate {
lifetime: clean_lifetime(wrp.lifetime, cx),
bounds: wrp.bounds.iter().filter_map(|x| x.clean(cx)).collect(),
bounds: wrp.bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(),
},

hir::WherePredicate::EqPredicate(ref wrp) => WherePredicate::EqPredicate {
Expand Down Expand Up @@ -349,7 +343,7 @@ fn clean_poly_trait_predicate<'tcx>(
let poly_trait_ref = pred.map_bound(|pred| pred.trait_ref);
Some(WherePredicate::BoundPredicate {
ty: clean_middle_ty(poly_trait_ref.skip_binder().self_ty(), cx, None),
bounds: vec![poly_trait_ref.clean(cx)],
bounds: vec![clean_poly_trait_ref_with_bindings(cx, poly_trait_ref, &[])],
bound_params: Vec::new(),
})
}
Expand Down Expand Up @@ -531,7 +525,7 @@ fn clean_generic_param<'tcx>(
.bounds_for_param(did)
.filter(|bp| bp.origin != PredicateOrigin::WhereClause)
.flat_map(|bp| bp.bounds)
.filter_map(|x| x.clean(cx))
.filter_map(|x| clean_generic_bound(x, cx))
.collect()
} else {
Vec::new()
Expand Down Expand Up @@ -1041,7 +1035,7 @@ fn clean_trait_item<'tcx>(trait_item: &hir::TraitItem<'tcx>, cx: &mut DocContext
}
hir::TraitItemKind::Type(bounds, Some(default)) => {
let generics = enter_impl_trait(cx, |cx| trait_item.generics.clean(cx));
let bounds = bounds.iter().filter_map(|x| x.clean(cx)).collect();
let bounds = bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect();
let item_type = clean_middle_ty(hir_ty_to_ty(cx.tcx, default), cx, None);
AssocTypeItem(
Box::new(Typedef {
Expand All @@ -1054,7 +1048,7 @@ fn clean_trait_item<'tcx>(trait_item: &hir::TraitItem<'tcx>, cx: &mut DocContext
}
hir::TraitItemKind::Type(bounds, None) => {
let generics = enter_impl_trait(cx, |cx| trait_item.generics.clean(cx));
let bounds = bounds.iter().filter_map(|x| x.clean(cx)).collect();
let bounds = bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect();
TyAssocTypeItem(Box::new(generics), bounds)
}
};
Expand Down Expand Up @@ -1507,7 +1501,7 @@ pub(crate) fn clean_ty<'tcx>(ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> T
TyKind::OpaqueDef(item_id, _) => {
let item = cx.tcx.hir().item(item_id);
if let hir::ItemKind::OpaqueTy(ref ty) = item.kind {
ImplTrait(ty.bounds.iter().filter_map(|x| x.clean(cx)).collect())
ImplTrait(ty.bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect())
} else {
unreachable!()
}
Expand Down Expand Up @@ -1911,7 +1905,7 @@ fn clean_maybe_renamed_item<'tcx>(
kind: ConstantKind::Local { body: body_id, def_id },
}),
ItemKind::OpaqueTy(ref ty) => OpaqueTyItem(OpaqueTy {
bounds: ty.bounds.iter().filter_map(|x| x.clean(cx)).collect(),
bounds: ty.bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(),
generics: ty.generics.clean(cx),
}),
ItemKind::TyAlias(hir_ty, generics) => {
Expand All @@ -1929,7 +1923,7 @@ fn clean_maybe_renamed_item<'tcx>(
}),
ItemKind::TraitAlias(generics, bounds) => TraitAliasItem(TraitAlias {
generics: generics.clean(cx),
bounds: bounds.iter().filter_map(|x| x.clean(cx)).collect(),
bounds: bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(),
}),
ItemKind::Union(ref variant_data, generics) => UnionItem(Union {
generics: generics.clean(cx),
Expand Down Expand Up @@ -1961,7 +1955,7 @@ fn clean_maybe_renamed_item<'tcx>(
def_id,
items,
generics: generics.clean(cx),
bounds: bounds.iter().filter_map(|x| x.clean(cx)).collect(),
bounds: bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(),
})
}
ItemKind::ExternCrate(orig_name) => {
Expand Down Expand Up @@ -2241,7 +2235,7 @@ fn clean_type_binding<'tcx>(
TypeBindingKind::Equality { term: clean_hir_term(term, cx) }
}
hir::TypeBindingKind::Constraint { bounds } => TypeBindingKind::Constraint {
bounds: bounds.iter().filter_map(|b| b.clean(cx)).collect(),
bounds: bounds.iter().filter_map(|b| clean_generic_bound(b, cx)).collect(),
},
},
}
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/did_you_mean/use_instead_of_import.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ use std::{
rc::Rc,
};

use std::time::Duration;
//~^ ERROR expected item, found `require`

use std::time::Instant;
//~^ ERROR expected item, found `include`

pub use std::io;
//~^ ERROR expected item, found `using`

fn main() {
let x = Rc::new(1);
let _ = write!(io::stdout(), "{:?}", x);
let _ = Duration::new(5, 0);
let _ = Instant::now();
}
8 changes: 8 additions & 0 deletions src/test/ui/did_you_mean/use_instead_of_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ import std::{
rc::Rc,
};

require std::time::Duration;
//~^ ERROR expected item, found `require`

include std::time::Instant;
//~^ ERROR expected item, found `include`

pub using std::io;
//~^ ERROR expected item, found `using`

fn main() {
let x = Rc::new(1);
let _ = write!(io::stdout(), "{:?}", x);
let _ = Duration::new(5, 0);
let _ = Instant::now();
}
16 changes: 14 additions & 2 deletions src/test/ui/did_you_mean/use_instead_of_import.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@ error: expected item, found `import`
LL | import std::{
| ^^^^^^ help: items are imported using the `use` keyword

error: expected item, found `require`
--> $DIR/use_instead_of_import.rs:9:1
|
LL | require std::time::Duration;
| ^^^^^^^ help: items are imported using the `use` keyword

error: expected item, found `include`
--> $DIR/use_instead_of_import.rs:12:1
|
LL | include std::time::Instant;
| ^^^^^^^ help: items are imported using the `use` keyword

error: expected item, found `using`
--> $DIR/use_instead_of_import.rs:9:5
--> $DIR/use_instead_of_import.rs:15:5
|
LL | pub using std::io;
| ^^^^^ help: items are imported using the `use` keyword

error: aborting due to 2 previous errors
error: aborting due to 4 previous errors

8 changes: 8 additions & 0 deletions src/test/ui/for-loop-while/break-while-condition.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ LL | | }
|
= note: expected type `!`
found unit type `()`
note: the function expects a value to always be returned, but loops might run zero times
--> $DIR/break-while-condition.rs:24:13
|
LL | while false {
| ^^^^^^^^^^^ this might have zero elements to iterate on
LL | return
| ------ if the loop doesn't execute, this value would never get returned
= help: return a value for the case when the loop has zero elements to iterate on, or consider changing the return type to account for that possibility

error: aborting due to 3 previous errors

Expand Down
15 changes: 15 additions & 0 deletions src/test/ui/let-else/issue-100103.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// edition:2021
// check-pass

#![feature(try_blocks)]
#![feature(let_else)]

fn main() {
let _: Result<i32, i32> = try {
let Some(x) = Some(0) else {
Err(1)?
};

x
};
}
Loading

0 comments on commit 5c54be3

Please sign in to comment.