Skip to content

Commit 595bc6f

Browse files
committed
Auto merge of rust-lang#119634 - matthiaskrgr:rollup-v2xt7et, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - rust-lang#119151 (Hide foreign `#[doc(hidden)]` paths in import suggestions) - rust-lang#119350 (Imply outlives-bounds on lazy type aliases) - rust-lang#119354 (Make `negative_bounds` internal & fix some of its issues) - rust-lang#119506 (Use `resolutions(()).effective_visiblities` to avoid cycle errors in `report_object_error`) - rust-lang#119554 (Fix scoping for let chains in match guards) - rust-lang#119563 (Check yield terminator's resume type in borrowck) - rust-lang#119589 (cstore: Remove unnecessary locking from `CrateMetadata`) - rust-lang#119622 (never patterns: Document behavior of never patterns with macros-by-example) r? `@ghost` `@rustbot` modify labels: rollup
2 parents b8c2074 + a060ed2 commit 595bc6f

File tree

84 files changed

+1272
-609
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+1272
-609
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -546,20 +546,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
546546

547547
fn lower_arm(&mut self, arm: &Arm) -> hir::Arm<'hir> {
548548
let pat = self.lower_pat(&arm.pat);
549-
let guard = arm.guard.as_ref().map(|cond| {
550-
if let ExprKind::Let(pat, scrutinee, span, is_recovered) = &cond.kind {
551-
hir::Guard::IfLet(self.arena.alloc(hir::Let {
552-
hir_id: self.next_id(),
553-
span: self.lower_span(*span),
554-
pat: self.lower_pat(pat),
555-
ty: None,
556-
init: self.lower_expr(scrutinee),
557-
is_recovered: *is_recovered,
558-
}))
559-
} else {
560-
hir::Guard::If(self.lower_expr(cond))
561-
}
562-
});
549+
let guard = arm.guard.as_ref().map(|cond| self.lower_expr(cond));
563550
let hir_id = self.next_id();
564551
let span = self.lower_span(arm.span);
565552
self.lower_attrs(hir_id, &arm.attrs);

compiler/rustc_ast_passes/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ ast_passes_module_nonascii = trying to load file for module `{$name}` with non-a
188188
ast_passes_negative_bound_not_supported =
189189
negative bounds are not supported
190190
191+
ast_passes_negative_bound_with_parenthetical_notation =
192+
parenthetical notation may not be used for negative bounds
193+
191194
ast_passes_nested_impl_trait = nested `impl Trait` is not allowed
192195
.outer = outer `impl Trait`
193196
.inner = nested `impl Trait` here

compiler/rustc_ast_passes/src/ast_validation.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -1312,13 +1312,24 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
13121312
if let GenericBound::Trait(trait_ref, modifiers) = bound
13131313
&& let BoundPolarity::Negative(_) = modifiers.polarity
13141314
&& let Some(segment) = trait_ref.trait_ref.path.segments.last()
1315-
&& let Some(ast::GenericArgs::AngleBracketed(args)) = segment.args.as_deref()
13161315
{
1317-
for arg in &args.args {
1318-
if let ast::AngleBracketedArg::Constraint(constraint) = arg {
1319-
self.dcx()
1320-
.emit_err(errors::ConstraintOnNegativeBound { span: constraint.span });
1316+
match segment.args.as_deref() {
1317+
Some(ast::GenericArgs::AngleBracketed(args)) => {
1318+
for arg in &args.args {
1319+
if let ast::AngleBracketedArg::Constraint(constraint) = arg {
1320+
self.dcx().emit_err(errors::ConstraintOnNegativeBound {
1321+
span: constraint.span,
1322+
});
1323+
}
1324+
}
1325+
}
1326+
// The lowered form of parenthesized generic args contains a type binding.
1327+
Some(ast::GenericArgs::Parenthesized(args)) => {
1328+
self.dcx().emit_err(errors::NegativeBoundWithParentheticalNotation {
1329+
span: args.span,
1330+
});
13211331
}
1332+
None => {}
13221333
}
13231334
}
13241335

compiler/rustc_ast_passes/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,13 @@ pub struct ConstraintOnNegativeBound {
763763
pub span: Span,
764764
}
765765

766+
#[derive(Diagnostic)]
767+
#[diag(ast_passes_negative_bound_with_parenthetical_notation)]
768+
pub struct NegativeBoundWithParentheticalNotation {
769+
#[primary_span]
770+
pub span: Span,
771+
}
772+
766773
#[derive(Diagnostic)]
767774
#[diag(ast_passes_invalid_unnamed_field_ty)]
768775
pub struct InvalidUnnamedFieldTy {

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3590,7 +3590,7 @@ impl<'b, 'v> Visitor<'v> for ConditionVisitor<'b> {
35903590
));
35913591
} else if let Some(guard) = &arm.guard {
35923592
self.errors.push((
3593-
arm.pat.span.to(guard.body().span),
3593+
arm.pat.span.to(guard.span),
35943594
format!(
35953595
"if this pattern and condition are matched, {} is not \
35963596
initialized",

compiler/rustc_borrowck/src/type_check/input_output.rs

+12-21
Original file line numberDiff line numberDiff line change
@@ -94,31 +94,22 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
9494
);
9595
}
9696

97-
debug!(
98-
"equate_inputs_and_outputs: body.yield_ty {:?}, universal_regions.yield_ty {:?}",
99-
body.yield_ty(),
100-
universal_regions.yield_ty
101-
);
102-
103-
// We will not have a universal_regions.yield_ty if we yield (by accident)
104-
// outside of a coroutine and return an `impl Trait`, so emit a span_delayed_bug
105-
// because we don't want to panic in an assert here if we've already got errors.
106-
if body.yield_ty().is_some() != universal_regions.yield_ty.is_some() {
107-
self.tcx().dcx().span_delayed_bug(
108-
body.span,
109-
format!(
110-
"Expected body to have yield_ty ({:?}) iff we have a UR yield_ty ({:?})",
111-
body.yield_ty(),
112-
universal_regions.yield_ty,
113-
),
97+
if let Some(mir_yield_ty) = body.yield_ty() {
98+
let yield_span = body.local_decls[RETURN_PLACE].source_info.span;
99+
self.equate_normalized_input_or_output(
100+
universal_regions.yield_ty.unwrap(),
101+
mir_yield_ty,
102+
yield_span,
114103
);
115104
}
116105

117-
if let (Some(mir_yield_ty), Some(ur_yield_ty)) =
118-
(body.yield_ty(), universal_regions.yield_ty)
119-
{
106+
if let Some(mir_resume_ty) = body.resume_ty() {
120107
let yield_span = body.local_decls[RETURN_PLACE].source_info.span;
121-
self.equate_normalized_input_or_output(ur_yield_ty, mir_yield_ty, yield_span);
108+
self.equate_normalized_input_or_output(
109+
universal_regions.resume_ty.unwrap(),
110+
mir_resume_ty,
111+
yield_span,
112+
);
122113
}
123114

124115
// Return types are a bit more complex. They may contain opaque `impl Trait` types.

compiler/rustc_borrowck/src/type_check/liveness/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ impl<'cx, 'tcx> Visitor<'tcx> for LiveVariablesVisitor<'cx, 'tcx> {
183183
match ty_context {
184184
TyContext::ReturnTy(SourceInfo { span, .. })
185185
| TyContext::YieldTy(SourceInfo { span, .. })
186+
| TyContext::ResumeTy(SourceInfo { span, .. })
186187
| TyContext::UserTy(span)
187188
| TyContext::LocalDecl { source_info: SourceInfo { span, .. }, .. } => {
188189
span_bug!(span, "should not be visiting outside of the CFG: {:?}", ty_context);

compiler/rustc_borrowck/src/type_check/mod.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -1450,13 +1450,13 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
14501450
}
14511451
}
14521452
}
1453-
TerminatorKind::Yield { value, .. } => {
1453+
TerminatorKind::Yield { value, resume_arg, .. } => {
14541454
self.check_operand(value, term_location);
14551455

1456-
let value_ty = value.ty(body, tcx);
14571456
match body.yield_ty() {
14581457
None => span_mirbug!(self, term, "yield in non-coroutine"),
14591458
Some(ty) => {
1459+
let value_ty = value.ty(body, tcx);
14601460
if let Err(terr) = self.sub_types(
14611461
value_ty,
14621462
ty,
@@ -1474,6 +1474,28 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
14741474
}
14751475
}
14761476
}
1477+
1478+
match body.resume_ty() {
1479+
None => span_mirbug!(self, term, "yield in non-coroutine"),
1480+
Some(ty) => {
1481+
let resume_ty = resume_arg.ty(body, tcx);
1482+
if let Err(terr) = self.sub_types(
1483+
ty,
1484+
resume_ty.ty,
1485+
term_location.to_locations(),
1486+
ConstraintCategory::Yield,
1487+
) {
1488+
span_mirbug!(
1489+
self,
1490+
term,
1491+
"type of resume place is {:?}, but the resume type is {:?}: {:?}",
1492+
resume_ty,
1493+
ty,
1494+
terr
1495+
);
1496+
}
1497+
}
1498+
}
14771499
}
14781500
}
14791501
}

compiler/rustc_borrowck/src/universal_regions.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ pub struct UniversalRegions<'tcx> {
7676
pub unnormalized_input_tys: &'tcx [Ty<'tcx>],
7777

7878
pub yield_ty: Option<Ty<'tcx>>,
79+
80+
pub resume_ty: Option<Ty<'tcx>>,
7981
}
8082

8183
/// The "defining type" for this MIR. The key feature of the "defining
@@ -525,9 +527,12 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
525527
debug!("build: extern regions = {}..{}", first_extern_index, first_local_index);
526528
debug!("build: local regions = {}..{}", first_local_index, num_universals);
527529

528-
let yield_ty = match defining_ty {
529-
DefiningTy::Coroutine(_, args) => Some(args.as_coroutine().yield_ty()),
530-
_ => None,
530+
let (resume_ty, yield_ty) = match defining_ty {
531+
DefiningTy::Coroutine(_, args) => {
532+
let tys = args.as_coroutine();
533+
(Some(tys.resume_ty()), Some(tys.yield_ty()))
534+
}
535+
_ => (None, None),
531536
};
532537

533538
UniversalRegions {
@@ -541,6 +546,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
541546
unnormalized_output_ty: *unnormalized_output_ty,
542547
unnormalized_input_tys,
543548
yield_ty,
549+
resume_ty,
544550
}
545551
}
546552

compiler/rustc_feature/src/unstable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ declare_features! (
210210
/// Allows the `multiple_supertrait_upcastable` lint.
211211
(unstable, multiple_supertrait_upcastable, "1.69.0", None),
212212
/// Allow negative trait bounds. This is an internal-only feature for testing the trait solver!
213-
(incomplete, negative_bounds, "1.71.0", None),
213+
(internal, negative_bounds, "1.71.0", None),
214214
/// Allows using `#[omit_gdb_pretty_printer_section]`.
215215
(internal, omit_gdb_pretty_printer_section, "1.5.0", None),
216216
/// Allows using `#[prelude_import]` on glob `use` items.

compiler/rustc_hir/src/hir.rs

+1-21
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,7 @@ pub struct Arm<'hir> {
12581258
/// If this pattern and the optional guard matches, then `body` is evaluated.
12591259
pub pat: &'hir Pat<'hir>,
12601260
/// Optional guard clause.
1261-
pub guard: Option<Guard<'hir>>,
1261+
pub guard: Option<&'hir Expr<'hir>>,
12621262
/// The expression the arm evaluates to if this arm matches.
12631263
pub body: &'hir Expr<'hir>,
12641264
}
@@ -1280,26 +1280,6 @@ pub struct Let<'hir> {
12801280
pub is_recovered: Option<ErrorGuaranteed>,
12811281
}
12821282

1283-
#[derive(Debug, Clone, Copy, HashStable_Generic)]
1284-
pub enum Guard<'hir> {
1285-
If(&'hir Expr<'hir>),
1286-
IfLet(&'hir Let<'hir>),
1287-
}
1288-
1289-
impl<'hir> Guard<'hir> {
1290-
/// Returns the body of the guard
1291-
///
1292-
/// In other words, returns the e in either of the following:
1293-
///
1294-
/// - `if e`
1295-
/// - `if let x = e`
1296-
pub fn body(&self) -> &'hir Expr<'hir> {
1297-
match self {
1298-
Guard::If(e) | Guard::IfLet(Let { init: e, .. }) => e,
1299-
}
1300-
}
1301-
}
1302-
13031283
#[derive(Debug, Clone, Copy, HashStable_Generic)]
13041284
pub struct ExprField<'hir> {
13051285
#[stable_hasher(ignore)]

compiler/rustc_hir/src/intravisit.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -619,13 +619,8 @@ pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt<'v>) {
619619
pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm<'v>) {
620620
visitor.visit_id(arm.hir_id);
621621
visitor.visit_pat(arm.pat);
622-
if let Some(ref g) = arm.guard {
623-
match g {
624-
Guard::If(ref e) => visitor.visit_expr(e),
625-
Guard::IfLet(ref l) => {
626-
visitor.visit_let_expr(l);
627-
}
628-
}
622+
if let Some(ref e) = arm.guard {
623+
visitor.visit_expr(e);
629624
}
630625
visitor.visit_expr(arm.body);
631626
}

compiler/rustc_hir_analysis/src/astconv/bounds.rs

+30-22
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,36 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
2626
span: Span,
2727
) {
2828
let tcx = self.tcx();
29+
let sized_def_id = tcx.lang_items().sized_trait();
30+
let mut seen_negative_sized_bound = false;
2931

3032
// Try to find an unbound in bounds.
3133
let mut unbounds: SmallVec<[_; 1]> = SmallVec::new();
3234
let mut search_bounds = |ast_bounds: &'tcx [hir::GenericBound<'tcx>]| {
3335
for ab in ast_bounds {
34-
if let hir::GenericBound::Trait(ptr, hir::TraitBoundModifier::Maybe) = ab {
35-
unbounds.push(ptr)
36+
let hir::GenericBound::Trait(ptr, modifier) = ab else {
37+
continue;
38+
};
39+
match modifier {
40+
hir::TraitBoundModifier::Maybe => unbounds.push(ptr),
41+
hir::TraitBoundModifier::Negative => {
42+
if let Some(sized_def_id) = sized_def_id
43+
&& ptr.trait_ref.path.res == Res::Def(DefKind::Trait, sized_def_id)
44+
{
45+
seen_negative_sized_bound = true;
46+
}
47+
}
48+
_ => {}
3649
}
3750
}
3851
};
3952
search_bounds(ast_bounds);
4053
if let Some((self_ty, where_clause)) = self_ty_where_predicates {
4154
for clause in where_clause {
42-
if let hir::WherePredicate::BoundPredicate(pred) = clause {
43-
if pred.is_param_bound(self_ty.to_def_id()) {
44-
search_bounds(pred.bounds);
45-
}
55+
if let hir::WherePredicate::BoundPredicate(pred) = clause
56+
&& pred.is_param_bound(self_ty.to_def_id())
57+
{
58+
search_bounds(pred.bounds);
4659
}
4760
}
4861
}
@@ -53,15 +66,13 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
5366
});
5467
}
5568

56-
let sized_def_id = tcx.lang_items().sized_trait();
57-
5869
let mut seen_sized_unbound = false;
5970
for unbound in unbounds {
60-
if let Some(sized_def_id) = sized_def_id {
61-
if unbound.trait_ref.path.res == Res::Def(DefKind::Trait, sized_def_id) {
62-
seen_sized_unbound = true;
63-
continue;
64-
}
71+
if let Some(sized_def_id) = sized_def_id
72+
&& unbound.trait_ref.path.res == Res::Def(DefKind::Trait, sized_def_id)
73+
{
74+
seen_sized_unbound = true;
75+
continue;
6576
}
6677
// There was a `?Trait` bound, but it was not `?Sized`; warn.
6778
tcx.dcx().span_warn(
@@ -71,15 +82,12 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
7182
);
7283
}
7384

74-
// If the above loop finished there was no `?Sized` bound; add implicitly sized if `Sized` is available.
75-
if sized_def_id.is_none() {
76-
// No lang item for `Sized`, so we can't add it as a bound.
77-
return;
78-
}
79-
if seen_sized_unbound {
80-
// There was in fact a `?Sized` bound, return without doing anything
81-
} else {
82-
// There was no `?Sized` bound; add implicitly sized if `Sized` is available.
85+
if seen_sized_unbound || seen_negative_sized_bound {
86+
// There was in fact a `?Sized` or `!Sized` bound;
87+
// we don't need to do anything.
88+
} else if sized_def_id.is_some() {
89+
// There was no `?Sized` or `!Sized` bound;
90+
// add `Sized` if it's available.
8391
bounds.push_sized(tcx, self_ty, span);
8492
}
8593
}

compiler/rustc_hir_analysis/src/check/region.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,24 @@ fn resolve_block<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, blk: &'tcx h
177177
}
178178

179179
fn resolve_arm<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, arm: &'tcx hir::Arm<'tcx>) {
180+
fn has_let_expr(expr: &Expr<'_>) -> bool {
181+
match &expr.kind {
182+
hir::ExprKind::Binary(_, lhs, rhs) => has_let_expr(lhs) || has_let_expr(rhs),
183+
hir::ExprKind::Let(..) => true,
184+
_ => false,
185+
}
186+
}
187+
180188
let prev_cx = visitor.cx;
181189

182190
visitor.terminating_scopes.insert(arm.hir_id.local_id);
183191

184192
visitor.enter_node_scope_with_dtor(arm.hir_id.local_id);
185193
visitor.cx.var_parent = visitor.cx.parent;
186194

187-
if let Some(hir::Guard::If(expr)) = arm.guard {
195+
if let Some(expr) = arm.guard
196+
&& !has_let_expr(expr)
197+
{
188198
visitor.terminating_scopes.insert(expr.hir_id.local_id);
189199
}
190200

0 commit comments

Comments
 (0)