diff --git a/compiler/rustc_hir_analysis/messages.ftl b/compiler/rustc_hir_analysis/messages.ftl index 64743e2abdcf7..e3b4ec9d0aaef 100644 --- a/compiler/rustc_hir_analysis/messages.ftl +++ b/compiler/rustc_hir_analysis/messages.ftl @@ -102,6 +102,9 @@ hir_analysis_invalid_union_field_sugg = hir_analysis_late_bound_const_in_apit = `impl Trait` can only mention const parameters from an fn or impl .label = const parameter declared here +hir_analysis_late_bound_lifetime_in_apit = `impl Trait` can only mention lifetimes from an fn or impl + .label = lifetime declared here + hir_analysis_late_bound_type_in_apit = `impl Trait` can only mention type parameters from an fn or impl .label = type parameter declared here diff --git a/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs b/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs index a5e2161732b10..523ca63e1daa7 100644 --- a/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs +++ b/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs @@ -1344,12 +1344,10 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> { Scope::Binder { where_bound_origin: Some(hir::PredicateOrigin::ImplTrait), .. } => { - let mut err = self.tcx.sess.struct_span_err( - lifetime_ref.ident.span, - "`impl Trait` can only mention lifetimes bound at the fn or impl level", - ); - err.span_note(self.tcx.def_span(region_def_id), "lifetime declared here"); - err.emit(); + self.tcx.sess.emit_err(errors::LateBoundInApit::Lifetime { + span: lifetime_ref.ident.span, + param_span: self.tcx.def_span(region_def_id), + }); return; } Scope::Root { .. } => break, diff --git a/compiler/rustc_hir_analysis/src/errors.rs b/compiler/rustc_hir_analysis/src/errors.rs index aa94f4dfa7c2a..cb840592edd22 100644 --- a/compiler/rustc_hir_analysis/src/errors.rs +++ b/compiler/rustc_hir_analysis/src/errors.rs @@ -892,4 +892,11 @@ pub(crate) enum LateBoundInApit { #[label] param_span: Span, }, + #[diag(hir_analysis_late_bound_lifetime_in_apit)] + Lifetime { + #[primary_span] + span: Span, + #[label] + param_span: Span, + }, } diff --git a/tests/ui/impl-trait/universal_wrong_hrtb.rs b/tests/ui/impl-trait/universal_wrong_hrtb.rs index b9551c2ceb0e5..48561710143b6 100644 --- a/tests/ui/impl-trait/universal_wrong_hrtb.rs +++ b/tests/ui/impl-trait/universal_wrong_hrtb.rs @@ -3,6 +3,6 @@ trait Trait<'a> { } fn test_argument_position(x: impl for<'a> Trait<'a, Assoc = impl Copy + 'a>) {} -//~^ ERROR `impl Trait` can only mention lifetimes bound at the fn or impl level +//~^ ERROR `impl Trait` can only mention lifetimes from an fn or impl fn main() {} diff --git a/tests/ui/impl-trait/universal_wrong_hrtb.stderr b/tests/ui/impl-trait/universal_wrong_hrtb.stderr index 37eb8dfa1a141..b5a091b61faf0 100644 --- a/tests/ui/impl-trait/universal_wrong_hrtb.stderr +++ b/tests/ui/impl-trait/universal_wrong_hrtb.stderr @@ -1,14 +1,8 @@ -error: `impl Trait` can only mention lifetimes bound at the fn or impl level +error: `impl Trait` can only mention lifetimes from an fn or impl --> $DIR/universal_wrong_hrtb.rs:5:73 | LL | fn test_argument_position(x: impl for<'a> Trait<'a, Assoc = impl Copy + 'a>) {} - | ^^ - | -note: lifetime declared here - --> $DIR/universal_wrong_hrtb.rs:5:39 - | -LL | fn test_argument_position(x: impl for<'a> Trait<'a, Assoc = impl Copy + 'a>) {} - | ^^ + | -- lifetime declared here ^^ error: aborting due to previous error