Skip to content

Commit 26cd548

Browse files
Account for late-bound vars from parent arg-position impl trait
1 parent 6f8c27a commit 26cd548

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

Diff for: compiler/rustc_hir_analysis/messages.ftl

+6
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ hir_analysis_invalid_union_field =
9999
hir_analysis_invalid_union_field_sugg =
100100
wrap the field type in `ManuallyDrop<...>`
101101
102+
hir_analysis_late_bound_const_in_apit = `impl Trait` can only mention const parameters from an fn or impl
103+
.label = const parameter declared here
104+
105+
hir_analysis_late_bound_type_in_apit = `impl Trait` can only mention type parameters from an fn or impl
106+
.label = type parameter declared here
107+
102108
hir_analysis_lifetimes_or_bounds_mismatch_on_trait =
103109
lifetime parameters or bounds on {$item_kind} `{$ident}` do not match the trait declaration
104110
.label = lifetimes do not match {$item_kind} in trait

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

+45
Original file line numberDiff line numberDiff line change
@@ -1379,6 +1379,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
13791379
let mut late_depth = 0;
13801380
let mut scope = self.scope;
13811381
let mut crossed_anon_const = false;
1382+
13821383
let result = loop {
13831384
match *scope {
13841385
Scope::Body { s, .. } => {
@@ -1446,6 +1447,50 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
14461447
return;
14471448
}
14481449

1450+
// We may fail to resolve higher-ranked ty/const vars that are mentioned by APIT.
1451+
// AST-based resolution does not care for impl-trait desugaring, which are the
1452+
// responsibility of lowering. This may create a mismatch between the resolution
1453+
// AST found (`param_def_id`) which points to HRTB, and what HIR allows.
1454+
// ```
1455+
// fn foo(x: impl for<T> Trait<Assoc = impl Trait2<T>>) {}
1456+
// ```
1457+
//
1458+
// In such case, walk back the binders to diagnose it properly.
1459+
let mut scope = self.scope;
1460+
loop {
1461+
match *scope {
1462+
Scope::Binder {
1463+
where_bound_origin: Some(hir::PredicateOrigin::ImplTrait), ..
1464+
} => {
1465+
let guar = self.tcx.sess.emit_err(match self.tcx.def_kind(param_def_id) {
1466+
DefKind::TyParam => errors::LateBoundInApit::Type {
1467+
span: self.tcx.hir().span(hir_id),
1468+
param_span: self.tcx.def_span(param_def_id),
1469+
},
1470+
DefKind::ConstParam => errors::LateBoundInApit::Const {
1471+
span: self.tcx.hir().span(hir_id),
1472+
param_span: self.tcx.def_span(param_def_id),
1473+
},
1474+
kind => {
1475+
bug!("unexpected def-kind: {}", kind.descr(param_def_id.to_def_id()))
1476+
}
1477+
});
1478+
self.map.defs.insert(hir_id, ResolvedArg::Error(guar));
1479+
return;
1480+
}
1481+
Scope::Root { .. } => break,
1482+
Scope::Binder { s, .. }
1483+
| Scope::Body { s, .. }
1484+
| Scope::Elision { s, .. }
1485+
| Scope::ObjectLifetimeDefault { s, .. }
1486+
| Scope::Supertrait { s, .. }
1487+
| Scope::TraitRefBoundary { s, .. }
1488+
| Scope::AnonConstBoundary { s } => {
1489+
scope = s;
1490+
}
1491+
}
1492+
}
1493+
14491494
self.tcx.sess.delay_span_bug(
14501495
self.tcx.hir().span(hir_id),
14511496
format!("could not resolve {param_def_id:?}"),

Diff for: compiler/rustc_hir_analysis/src/errors.rs

+18
Original file line numberDiff line numberDiff line change
@@ -875,3 +875,21 @@ pub(crate) enum ReturnTypeNotationIllegalParam {
875875
param_span: Span,
876876
},
877877
}
878+
879+
#[derive(Diagnostic)]
880+
pub(crate) enum LateBoundInApit {
881+
#[diag(hir_analysis_late_bound_type_in_apit)]
882+
Type {
883+
#[primary_span]
884+
span: Span,
885+
#[label]
886+
param_span: Span,
887+
},
888+
#[diag(hir_analysis_late_bound_const_in_apit)]
889+
Const {
890+
#[primary_span]
891+
span: Span,
892+
#[label]
893+
param_span: Span,
894+
},
895+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(non_lifetime_binders)]
2+
//~^ WARN the feature `non_lifetime_binders` is incomplete
3+
4+
trait Trait<Input> {
5+
type Assoc;
6+
}
7+
8+
fn uwu(_: impl for<T> Trait<(), Assoc = impl Trait<T>>) {}
9+
//~^ ERROR `impl Trait` can only mention type parameters from an fn or impl
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
warning: the feature `non_lifetime_binders` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/nested-apit-mentioning-outer-bound-var.rs:1:12
3+
|
4+
LL | #![feature(non_lifetime_binders)]
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #108185 <https://github.com/rust-lang/rust/issues/108185> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
error: `impl Trait` can only mention type parameters from an fn or impl
11+
--> $DIR/nested-apit-mentioning-outer-bound-var.rs:8:52
12+
|
13+
LL | fn uwu(_: impl for<T> Trait<(), Assoc = impl Trait<T>>) {}
14+
| - type parameter declared here ^
15+
16+
error: aborting due to previous error; 1 warning emitted
17+

0 commit comments

Comments
 (0)