From 3ae5fed04c94be2f8591c7f4ab76f78ec882cd32 Mon Sep 17 00:00:00 2001 From: Jack Huey Date: Tue, 6 Apr 2021 15:34:29 -0400 Subject: [PATCH] Fix a couple resolve bugs from binder refactor --- compiler/rustc_resolve/src/late/lifetimes.rs | 16 +++++++++++++++- ...3-invalid-associated-type-supertrait-hrtb.rs | 8 ++++++++ ...valid-associated-type-supertrait-hrtb.stderr | 9 +++++++++ .../issue-83907-invalid-fn-like-path.rs | 7 +++++++ .../issue-83907-invalid-fn-like-path.stderr | 17 +++++++++++++++++ 5 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/lifetimes/issue-83753-invalid-associated-type-supertrait-hrtb.rs create mode 100644 src/test/ui/lifetimes/issue-83753-invalid-associated-type-supertrait-hrtb.stderr create mode 100644 src/test/ui/lifetimes/issue-83907-invalid-fn-like-path.rs create mode 100644 src/test/ui/lifetimes/issue-83907-invalid-fn-like-path.stderr diff --git a/compiler/rustc_resolve/src/late/lifetimes.rs b/compiler/rustc_resolve/src/late/lifetimes.rs index b89ad867f46ee..70ddfcc9004f1 100644 --- a/compiler/rustc_resolve/src/late/lifetimes.rs +++ b/compiler/rustc_resolve/src/late/lifetimes.rs @@ -2659,6 +2659,13 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { Some(next) => next, None => break None, }; + // See issue #83753. If someone writes an associated type on a non-trait, just treat it as + // there being no supertrait HRTBs. + match tcx.def_kind(def_id) { + DefKind::Trait | DefKind::TraitAlias | DefKind::Impl => {} + _ => break None, + } + if trait_defines_associated_type_named(def_id) { break Some(bound_vars.into_iter().collect()); } @@ -2703,7 +2710,14 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { | Scope::Supertrait { ref s, .. } => { scope = *s; } - Scope::Root => bug!("In fn_like_elision without appropriate scope above"), + Scope::Root => { + // See issue #83907. Just bail out from looking inside. + self.tcx.sess.delay_span_bug( + rustc_span::DUMMY_SP, + "In fn_like_elision without appropriate scope above", + ); + return; + } } }; // While not strictly necessary, we gather anon lifetimes *before* actually diff --git a/src/test/ui/lifetimes/issue-83753-invalid-associated-type-supertrait-hrtb.rs b/src/test/ui/lifetimes/issue-83753-invalid-associated-type-supertrait-hrtb.rs new file mode 100644 index 0000000000000..7f0ea730dd37b --- /dev/null +++ b/src/test/ui/lifetimes/issue-83753-invalid-associated-type-supertrait-hrtb.rs @@ -0,0 +1,8 @@ +// check-fail + +struct Foo {} +impl Foo { + fn bar(foo: Foo) {} + //~^ associated type bindings are not allowed here +} +fn main() {} diff --git a/src/test/ui/lifetimes/issue-83753-invalid-associated-type-supertrait-hrtb.stderr b/src/test/ui/lifetimes/issue-83753-invalid-associated-type-supertrait-hrtb.stderr new file mode 100644 index 0000000000000..f7bdee6336e2d --- /dev/null +++ b/src/test/ui/lifetimes/issue-83753-invalid-associated-type-supertrait-hrtb.stderr @@ -0,0 +1,9 @@ +error[E0229]: associated type bindings are not allowed here + --> $DIR/issue-83753-invalid-associated-type-supertrait-hrtb.rs:5:21 + | +LL | fn bar(foo: Foo) {} + | ^^^^^^^^^^^^^^ associated type not allowed here + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0229`. diff --git a/src/test/ui/lifetimes/issue-83907-invalid-fn-like-path.rs b/src/test/ui/lifetimes/issue-83907-invalid-fn-like-path.rs new file mode 100644 index 0000000000000..604687ce71126 --- /dev/null +++ b/src/test/ui/lifetimes/issue-83907-invalid-fn-like-path.rs @@ -0,0 +1,7 @@ +// check-fail + +static STATIC_VAR_FIVE: &One(); +//~^ cannot find type +//~| free static item without body + +fn main() {} diff --git a/src/test/ui/lifetimes/issue-83907-invalid-fn-like-path.stderr b/src/test/ui/lifetimes/issue-83907-invalid-fn-like-path.stderr new file mode 100644 index 0000000000000..e57933da558fd --- /dev/null +++ b/src/test/ui/lifetimes/issue-83907-invalid-fn-like-path.stderr @@ -0,0 +1,17 @@ +error: free static item without body + --> $DIR/issue-83907-invalid-fn-like-path.rs:3:1 + | +LL | static STATIC_VAR_FIVE: &One(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- + | | + | help: provide a definition for the static: `= ;` + +error[E0412]: cannot find type `One` in this scope + --> $DIR/issue-83907-invalid-fn-like-path.rs:3:26 + | +LL | static STATIC_VAR_FIVE: &One(); + | ^^^ not found in this scope + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0412`.