diff --git a/compiler/rustc_error_messages/locales/en-US/typeck.ftl b/compiler/rustc_error_messages/locales/en-US/typeck.ftl index 6a3235fc772..e279b39a690 100644 --- a/compiler/rustc_error_messages/locales/en-US/typeck.ftl +++ b/compiler/rustc_error_messages/locales/en-US/typeck.ftl @@ -45,6 +45,7 @@ typeck-copy-impl-on-non-adt = typeck-trait-object-declared-with-no-traits = at least one trait is required for an object type + .alias-span = this alias does not contain a trait typeck-ambiguous-lifetime-bound = ambiguous lifetime bound, explicit lifetime bound required diff --git a/compiler/rustc_typeck/src/astconv/mod.rs b/compiler/rustc_typeck/src/astconv/mod.rs index a21e9f4a0a5..b0244a44ffe 100644 --- a/compiler/rustc_typeck/src/astconv/mod.rs +++ b/compiler/rustc_typeck/src/astconv/mod.rs @@ -1370,7 +1370,13 @@ trait here instead: `trait NewTrait: {} {{}}`", } if regular_traits.is_empty() && auto_traits.is_empty() { - tcx.sess.emit_err(TraitObjectDeclaredWithNoTraits { span }); + let trait_alias_span = bounds + .trait_bounds + .iter() + .map(|&(trait_ref, _, _)| trait_ref.def_id()) + .find(|&trait_ref| tcx.is_trait_alias(trait_ref)) + .map(|trait_ref| tcx.def_span(trait_ref)); + tcx.sess.emit_err(TraitObjectDeclaredWithNoTraits { span, trait_alias_span }); return tcx.ty_error(); } diff --git a/compiler/rustc_typeck/src/errors.rs b/compiler/rustc_typeck/src/errors.rs index 3d2f93537e4..d99ae35bb9a 100644 --- a/compiler/rustc_typeck/src/errors.rs +++ b/compiler/rustc_typeck/src/errors.rs @@ -103,6 +103,8 @@ pub struct CopyImplOnNonAdt { pub struct TraitObjectDeclaredWithNoTraits { #[primary_span] pub span: Span, + #[label = "alias-span"] + pub trait_alias_span: Option, } #[derive(SessionDiagnostic)] diff --git a/src/test/ui/traits/alias/only-maybe-bound.stderr b/src/test/ui/traits/alias/only-maybe-bound.stderr index 99589edb535..e9e846c2ff3 100644 --- a/src/test/ui/traits/alias/only-maybe-bound.stderr +++ b/src/test/ui/traits/alias/only-maybe-bound.stderr @@ -1,12 +1,18 @@ error[E0224]: at least one trait is required for an object type --> $DIR/only-maybe-bound.rs:13:12 | +LL | trait _1 = _0; + | -------------- this alias does not contain a trait +... LL | type _T0 = dyn _1; | ^^^^^^ error[E0224]: at least one trait is required for an object type --> $DIR/only-maybe-bound.rs:19:12 | +LL | trait _2 = _1 + _1; + | ------------------- this alias does not contain a trait +LL | LL | type _T1 = dyn _2; | ^^^^^^ diff --git a/src/test/ui/traits/issue-65673.stderr b/src/test/ui/traits/issue-65673.stderr index 79f071ba302..71f3a0e7c7c 100644 --- a/src/test/ui/traits/issue-65673.stderr +++ b/src/test/ui/traits/issue-65673.stderr @@ -1,6 +1,9 @@ error[E0224]: at least one trait is required for an object type --> $DIR/issue-65673.rs:9:16 | +LL | trait Alias = where T: Trait; + | -------------------------------- this alias does not contain a trait +... LL | type Ctx = dyn Alias; | ^^^^^^^^^^^^