Skip to content

Enhance Scope Enum with Late Types and Consts Handling #139320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions compiler/rustc_ast_lowering/src/impl_trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/// Returns `true` if the parent path contains impl trait syntax:
/// For example given `impl Bla<impl Foo>`, this function would
/// return true for the path for `impl Foo`
pub(crate) fn parent_contains_impl_trait(cx: &LoweringContext<'_>, path: &ast::Path) -> bool {
let ast::Path { span: path_span, segments, tokens: _ } = path;

if let Some(parent_path_span) = path_span.parent_callsite() {
return matches!(cx.source_map().span_to_snippet(parent_path_span), Ok(s) if s.starts_with("impl "));
}

// This can be from a parameter list:
// like in `fn foo(a: impl Bla<impl Foo<T>..`) somewhere
// in a block or other nested context.
let parent_node = cx.source_map().span_to_enclosing_node(*path_span).next();

if let Some(node) = parent_node {
let content_str = cx.source_map().span_to_snippet(node.span).unwrap_or_default();
let segments_strs =
segments.iter().map(|s| cx.source_map().span_to_snippet(s.span()).unwrap_or_default());

let path_str = segments_strs.collect::<Vec<_>>().join("::");
// Check if parent contains "impl Trait", except for the current path:
let impl_trait_pattern = format!("impl {}", path_str);
if content_str.contains("impl") && content_str.contains(&impl_trait_pattern) {
return true;
}
}

false
}
55 changes: 42 additions & 13 deletions compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ enum Scope<'a> {
s: ScopeRef<'a>,
what: &'static str,
deny_late_regions: bool,
deny_late_types_and_consts: bool,
},

Root {
Expand Down Expand Up @@ -190,10 +191,11 @@ impl<'a> Scope<'a> {
.field("s", &"..")
.finish(),
Self::TraitRefBoundary { s: _ } => f.debug_struct("TraitRefBoundary").finish(),
Self::LateBoundary { s: _, what, deny_late_regions } => f
Self::LateBoundary { s: _, what, deny_late_regions, deny_late_types_and_consts } => f
.debug_struct("LateBoundary")
.field("what", what)
.field("deny_late_regions", deny_late_regions)
.field("deny_late_types_and_consts", deny_late_types_and_consts)
.finish(),
Self::Root { opt_parent_item } => {
f.debug_struct("Root").field("opt_parent_item", &opt_parent_item).finish()
Expand Down Expand Up @@ -586,10 +588,9 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
this.with(scope, |this| {
let scope = Scope::LateBoundary {
s: this.scope,
what: "nested `impl Trait`",
// We can capture late-bound regions; we just don't duplicate
// lifetime or const params, so we can't allow those.
what: "associated type bounds",
deny_late_regions: false,
deny_late_types_and_consts: false,
};
this.with(scope, |this| intravisit::walk_opaque_ty(this, opaque))
})
Expand Down Expand Up @@ -809,8 +810,9 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
self.with(scope, |this| {
let scope = Scope::LateBoundary {
s: this.scope,
what: "`impl Trait` in binding",
deny_late_regions: true,
what: "nested `impl Trait`",
deny_late_regions: false,
deny_late_types_and_consts: true,
};
this.with(scope, |this| {
for bound in bounds {
Expand Down Expand Up @@ -999,7 +1001,12 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {

fn visit_anon_const(&mut self, c: &'tcx hir::AnonConst) {
self.with(
Scope::LateBoundary { s: self.scope, what: "constant", deny_late_regions: true },
Scope::LateBoundary {
s: self.scope,
what: "constant",
deny_late_regions: true,
deny_late_types_and_consts: true,
},
|this| {
intravisit::walk_anon_const(this, c);
},
Expand Down Expand Up @@ -1298,8 +1305,18 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
scope = s;
}

Scope::LateBoundary { s, what, deny_late_regions } => {
if deny_late_regions {
Scope::LateBoundary {
s,
what,
deny_late_regions: _,
deny_late_types_and_consts,
} => {
// For debugging purposes
debug!(
"LateBoundary in resolve_type_ref - what: {}, deny_late_types_and_consts: {}",
what, deny_late_types_and_consts
);
if deny_late_types_and_consts {
crossed_late_boundary = Some(what);
}
scope = s;
Expand Down Expand Up @@ -1518,8 +1535,20 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
scope = s;
}

Scope::LateBoundary { s, what, deny_late_regions: _ } => {
crossed_late_boundary = Some(what);
Scope::LateBoundary {
s,
what,
deny_late_regions: _,
deny_late_types_and_consts,
} => {
// For debugging purposes
debug!(
"LateBoundary in resolve_type_ref - what: {}, deny_late_types_and_consts: {}",
what, deny_late_types_and_consts
);
if deny_late_types_and_consts {
crossed_late_boundary = Some(what);
}
scope = s;
}
}
Expand Down Expand Up @@ -1763,8 +1792,8 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
// trait Foo<'a> {
// type Item: 'a;
// }
// ```
//
// //
//```
// but if we just have `type Item;`, then it would be
// `'static`. However, we don't get all of this logic correct.
//
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_hir_analysis/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,24 +647,24 @@ pub(crate) struct VariadicFunctionCompatibleConvention<'a> {

#[derive(Diagnostic)]
pub(crate) enum CannotCaptureLateBound {
#[diag(hir_analysis_cannot_capture_late_bound_ty)]
Type {
#[diag(hir_analysis_cannot_capture_late_bound_lifetime)]
Lifetime {
#[primary_span]
use_span: Span,
#[label]
def_span: Span,
what: &'static str,
},
#[diag(hir_analysis_cannot_capture_late_bound_const)]
Const {
#[diag(hir_analysis_cannot_capture_late_bound_ty)]
Type {
#[primary_span]
use_span: Span,
#[label]
def_span: Span,
what: &'static str,
},
#[diag(hir_analysis_cannot_capture_late_bound_lifetime)]
Lifetime {
#[diag(hir_analysis_cannot_capture_late_bound_const)]
Const {
#[primary_span]
use_span: Span,
#[label]
Expand Down
18 changes: 18 additions & 0 deletions tests/incremental/non-lifetime-binder-in-nested-impl-trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//@ revisions: cfail
//@ should-ice
//@ compile-flags: --edition=2021
//@ error-pattern: assertion failed

#![feature(non_lifetime_binders)]
#![feature(associated_type_defaults)]
#![allow(incomplete_features)]

trait Trait<T: ?Sized> {
type Assoc<'a> = i32;
}

fn produce() -> impl for<T> Trait<(), Assoc = impl Trait<T>> {
16
}

fn main() {}
2 changes: 1 addition & 1 deletion tests/ui/impl-trait/in-bindings/escaping-bound-var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ impl<'a> Foo<'a> for () {

fn main() {
let x: &dyn for<'a> Foo<'a, Out = impl Sized + 'a> = &();
//~^ ERROR cannot capture late-bound lifetime in `impl Trait` in binding
//~^ ERROR cannot capture late-bound lifetime in nested `impl Trait`
}
2 changes: 1 addition & 1 deletion tests/ui/impl-trait/in-bindings/escaping-bound-var.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: cannot capture late-bound lifetime in `impl Trait` in binding
error: cannot capture late-bound lifetime in nested `impl Trait`
--> $DIR/escaping-bound-var.rs:12:52
|
LL | let x: &dyn for<'a> Foo<'a, Out = impl Sized + 'a> = &();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ fn produce() -> impl for<T> Trait<(), Assoc = impl Trait<T>> {
//~^ ERROR associated type `Assoc` not found for `Trait`
//~| ERROR associated type `Assoc` not found for `Trait`
//~| the trait bound `{integer}: Trait<()>` is not satisfied
//~| ERROR cannot capture late-bound type parameter in nested `impl Trait`
16
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
error: cannot capture late-bound type parameter in nested `impl Trait`
--> $DIR/non-lifetime-binder-in-constraint.rs:6:58
|
LL | fn produce() -> impl for<T> Trait<(), Assoc = impl Trait<T>> {
| - parameter defined here ^

error[E0220]: associated type `Assoc` not found for `Trait`
--> $DIR/non-lifetime-binder-in-constraint.rs:6:39
|
Expand Down Expand Up @@ -33,7 +27,7 @@ help: this trait has no implementations, consider adding one
LL | trait Trait<T: ?Sized> {}
| ^^^^^^^^^^^^^^^^^^^^^^

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

Some errors have detailed explanations: E0220, E0277.
For more information about an error, try `rustc --explain E0220`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![allow(incomplete_features)]
#![feature(non_lifetime_binders)]

trait Trait<T> {}

fn f() -> impl for<T> Trait<impl Trait<T>> {
//~^ ERROR nested `impl Trait` is not allowed
//~| ERROR the trait bound `(): Trait<impl Trait<T>>` is not satisfied
()
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
error[E0666]: nested `impl Trait` is not allowed
--> $DIR/non-lifetime-binder-nested.rs:6:29
|
LL | fn f() -> impl for<T> Trait<impl Trait<T>> {
| ------------------^^^^^^^^^^^^^-
| | |
| | nested `impl Trait` here
| outer `impl Trait`

error[E0277]: the trait bound `(): Trait<impl Trait<T>>` is not satisfied
--> $DIR/non-lifetime-binder-nested.rs:6:11
|
LL | fn f() -> impl for<T> Trait<impl Trait<T>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Trait<impl Trait<T>>` is not implemented for `()`
...
LL | ()
| -- return type was inferred to be `()` here
|
help: this trait has no implementations, consider adding one
--> $DIR/non-lifetime-binder-nested.rs:4:1
|
LL | trait Trait<T> {}
| ^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0277, E0666.
For more information about an error, try `rustc --explain E0277`.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ trait Trait<T> {}

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

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,19 @@ LL | fn f() -> impl for<T> Trait<impl Trait<T>> {}
| | nested `impl Trait` here
| outer `impl Trait`

error: cannot capture late-bound type parameter in nested `impl Trait`
--> $DIR/non-lifetime-binder.rs:6:40
|
LL | fn f() -> impl for<T> Trait<impl Trait<T>> {}
| - ^
| |
| parameter defined here

error[E0277]: the trait bound `(): Trait<impl Trait<{type error}>>` is not satisfied
error[E0277]: the trait bound `(): Trait<impl Trait<T>>` is not satisfied
--> $DIR/non-lifetime-binder.rs:6:11
|
LL | fn f() -> impl for<T> Trait<impl Trait<T>> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Trait<impl Trait<{type error}>>` is not implemented for `()`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Trait<impl Trait<T>>` is not implemented for `()`
|
help: this trait has no implementations, consider adding one
--> $DIR/non-lifetime-binder.rs:4:1
|
LL | trait Trait<T> {}
| ^^^^^^^^^^^^^^

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

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