Skip to content

Commit a1c98ca

Browse files
authored
Rollup merge of rust-lang#132832 - compiler-errors:late-ty, r=cjgillot
Deny capturing late-bound ty/const params in nested opaques First, this reverts a7f6095. I can't exactly remember why I approved this specific bit of rust-lang#132466; specifically, I don't know that the purpose of that commit is, and afaict we will never have an opaque that captures late-bound params through a const because opaques can't be used inside of anon consts. Am I missing something `@cjgillot?` Since I can't see a case where this matters, and no tests seem to fail. The second commit adds a `deny_late_regions: bool` to distinguish `Scope::LateBoundary` which should deny *any* late-bound params or just ty/consts. Then, when resolving opaques we wrap ourselves in a `Scope::LateBoundary { deny_late_regions: false }` so that we deny late-bound ty/const, which fixes a bunch of ICEs that all vaguely look like `impl for<T> Trait<Assoc = impl OtherTrait<T>>`. I guess this could be achieved other ways; for example, with a different scope kind, or maybe we could just reuse `Scope::Opaque`. But this seems a bit more verbose. I'm open to feedback anyways. Fixes rust-lang#131535 Fixes rust-lang#131637 Fixes rust-lang#132530 I opted to remove those crashes tests ^ without adding them as regular tests, since they're basically triggering uninteresting late-bound ICEs far off in the trait solver, and the reason that existing tests such as `tests/ui/type-alias-impl-trait/non-lifetime-binder-in-constraint.rs` don't ICE are kinda just coincidental (i.e. due to a missing impl block). I don't really feel motivated to add random permutations to tests just to exercise non-lifetime binders. r? cjgillot
2 parents 6b47c6d + 8d871b7 commit a1c98ca

File tree

8 files changed

+50
-47
lines changed

8 files changed

+50
-47
lines changed

Diff for: compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+29-22
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ enum Scope<'a> {
177177
LateBoundary {
178178
s: ScopeRef<'a>,
179179
what: &'static str,
180+
deny_late_regions: bool,
180181
},
181182

182183
Root {
@@ -234,9 +235,11 @@ impl<'a> fmt::Debug for TruncatedScopeDebug<'a> {
234235
.field("s", &"..")
235236
.finish(),
236237
Scope::TraitRefBoundary { s: _ } => f.debug_struct("TraitRefBoundary").finish(),
237-
Scope::LateBoundary { s: _, what } => {
238-
f.debug_struct("LateBoundary").field("what", what).finish()
239-
}
238+
Scope::LateBoundary { s: _, what, deny_late_regions } => f
239+
.debug_struct("LateBoundary")
240+
.field("what", what)
241+
.field("deny_late_regions", deny_late_regions)
242+
.finish(),
240243
Scope::Root { opt_parent_item } => {
241244
f.debug_struct("Root").field("opt_parent_item", &opt_parent_item).finish()
242245
}
@@ -573,17 +576,11 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
573576
// give, we will reverse the IndexMap after early captures.
574577
let mut late_depth = 0;
575578
let mut scope = self.scope;
576-
let mut crossed_late_boundary = None;
577579
let mut opaque_capture_scopes = vec![(opaque.def_id, &captures)];
578580
loop {
579581
match *scope {
580582
Scope::Binder { ref bound_vars, scope_type, s, .. } => {
581583
for (&original_lifetime, &def) in bound_vars.iter().rev() {
582-
if let ResolvedArg::LateBound(..) = def
583-
&& crossed_late_boundary.is_some()
584-
{
585-
continue;
586-
}
587584
if let DefKind::LifetimeParam = self.tcx.def_kind(original_lifetime) {
588585
let def = def.shifted(late_depth);
589586
let ident = lifetime_ident(original_lifetime);
@@ -624,12 +621,8 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
624621

625622
Scope::ObjectLifetimeDefault { s, .. }
626623
| Scope::Supertrait { s, .. }
627-
| Scope::TraitRefBoundary { s, .. } => {
628-
scope = s;
629-
}
630-
631-
Scope::LateBoundary { s, what, .. } => {
632-
crossed_late_boundary = Some(what);
624+
| Scope::TraitRefBoundary { s, .. }
625+
| Scope::LateBoundary { s, .. } => {
633626
scope = s;
634627
}
635628
}
@@ -640,7 +633,16 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
640633
let scope = Scope::Opaque { captures: &captures, def_id: opaque.def_id, s: self.scope };
641634
self.with(scope, |this| {
642635
let scope = Scope::TraitRefBoundary { s: this.scope };
643-
this.with(scope, |this| intravisit::walk_opaque_ty(this, opaque))
636+
this.with(scope, |this| {
637+
let scope = Scope::LateBoundary {
638+
s: this.scope,
639+
what: "nested `impl Trait`",
640+
// We can capture late-bound regions; we just don't duplicate
641+
// lifetime or const params, so we can't allow those.
642+
deny_late_regions: false,
643+
};
644+
this.with(scope, |this| intravisit::walk_opaque_ty(this, opaque))
645+
})
644646
});
645647

646648
let captures = captures.into_inner().into_iter().collect();
@@ -997,9 +999,12 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
997999
}
9981000

9991001
fn visit_anon_const(&mut self, c: &'tcx hir::AnonConst) {
1000-
self.with(Scope::LateBoundary { s: self.scope, what: "constant" }, |this| {
1001-
intravisit::walk_anon_const(this, c);
1002-
});
1002+
self.with(
1003+
Scope::LateBoundary { s: self.scope, what: "constant", deny_late_regions: true },
1004+
|this| {
1005+
intravisit::walk_anon_const(this, c);
1006+
},
1007+
);
10031008
}
10041009

10051010
fn visit_generic_param(&mut self, p: &'tcx GenericParam<'tcx>) {
@@ -1291,8 +1296,10 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
12911296
scope = s;
12921297
}
12931298

1294-
Scope::LateBoundary { s, what } => {
1295-
crossed_late_boundary = Some(what);
1299+
Scope::LateBoundary { s, what, deny_late_regions } => {
1300+
if deny_late_regions {
1301+
crossed_late_boundary = Some(what);
1302+
}
12961303
scope = s;
12971304
}
12981305
}
@@ -1508,7 +1515,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
15081515
scope = s;
15091516
}
15101517

1511-
Scope::LateBoundary { s, what } => {
1518+
Scope::LateBoundary { s, what, deny_late_regions: _ } => {
15121519
crossed_late_boundary = Some(what);
15131520
scope = s;
15141521
}

Diff for: tests/crashes/131535.rs

-4
This file was deleted.

Diff for: tests/crashes/131637.rs

-7
This file was deleted.

Diff for: tests/crashes/132530.rs

-9
This file was deleted.

Diff for: tests/ui/type-alias-impl-trait/non-lifetime-binder-in-constraint.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ fn produce() -> impl for<T> Trait<(), Assoc = impl Trait<T>> {
77
//~^ ERROR associated type `Assoc` not found for `Trait`
88
//~| ERROR associated type `Assoc` not found for `Trait`
99
//~| the trait bound `{integer}: Trait<()>` is not satisfied
10+
//~| ERROR cannot capture late-bound type parameter in nested `impl Trait`
1011
16
1112
}
1213

Diff for: tests/ui/type-alias-impl-trait/non-lifetime-binder-in-constraint.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
error: cannot capture late-bound type parameter in nested `impl Trait`
2+
--> $DIR/non-lifetime-binder-in-constraint.rs:6:58
3+
|
4+
LL | fn produce() -> impl for<T> Trait<(), Assoc = impl Trait<T>> {
5+
| - parameter defined here ^
6+
17
error[E0220]: associated type `Assoc` not found for `Trait`
28
--> $DIR/non-lifetime-binder-in-constraint.rs:6:39
39
|
@@ -27,7 +33,7 @@ help: this trait has no implementations, consider adding one
2733
LL | trait Trait<T: ?Sized> {}
2834
| ^^^^^^^^^^^^^^^^^^^^^^
2935

30-
error: aborting due to 3 previous errors
36+
error: aborting due to 4 previous errors
3137

3238
Some errors have detailed explanations: E0220, E0277.
3339
For more information about an error, try `rustc --explain E0220`.

Diff for: tests/ui/type-alias-impl-trait/non-lifetime-binder.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ trait Trait<T> {}
55

66
fn f() -> impl for<T> Trait<impl Trait<T>> {}
77
//~^ ERROR nested `impl Trait` is not allowed
8-
//~| ERROR the trait bound `(): Trait<impl Trait<T>>` is not satisfied
8+
//~| ERROR the trait bound `(): Trait<impl Trait<{type error}>>` is not satisfied
9+
//~| ERROR cannot capture late-bound type parameter in nested `impl Trait`
910

1011
fn main() {}

Diff for: tests/ui/type-alias-impl-trait/non-lifetime-binder.stderr

+11-3
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,27 @@ LL | fn f() -> impl for<T> Trait<impl Trait<T>> {}
77
| | nested `impl Trait` here
88
| outer `impl Trait`
99

10-
error[E0277]: the trait bound `(): Trait<impl Trait<T>>` is not satisfied
10+
error: cannot capture late-bound type parameter in nested `impl Trait`
11+
--> $DIR/non-lifetime-binder.rs:6:40
12+
|
13+
LL | fn f() -> impl for<T> Trait<impl Trait<T>> {}
14+
| - ^
15+
| |
16+
| parameter defined here
17+
18+
error[E0277]: the trait bound `(): Trait<impl Trait<{type error}>>` is not satisfied
1119
--> $DIR/non-lifetime-binder.rs:6:11
1220
|
1321
LL | fn f() -> impl for<T> Trait<impl Trait<T>> {}
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Trait<impl Trait<T>>` is not implemented for `()`
22+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Trait<impl Trait<{type error}>>` is not implemented for `()`
1523
|
1624
help: this trait has no implementations, consider adding one
1725
--> $DIR/non-lifetime-binder.rs:4:1
1826
|
1927
LL | trait Trait<T> {}
2028
| ^^^^^^^^^^^^^^
2129

22-
error: aborting due to 2 previous errors
30+
error: aborting due to 3 previous errors
2331

2432
Some errors have detailed explanations: E0277, E0666.
2533
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)