Skip to content

Commit

Permalink
Point to the empty trait alias.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed May 9, 2022
1 parent 1d43b4d commit e947fad
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions compiler/rustc_error_messages/locales/en-US/typeck.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_typeck/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,13 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
}

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();
}

Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_typeck/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ pub struct CopyImplOnNonAdt {
pub struct TraitObjectDeclaredWithNoTraits {
#[primary_span]
pub span: Span,
#[label = "alias-span"]
pub trait_alias_span: Option<Span>,
}

#[derive(SessionDiagnostic)]
Expand Down
6 changes: 6 additions & 0 deletions src/test/ui/traits/alias/only-maybe-bound.stderr
Original file line number Diff line number Diff line change
@@ -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;
| ^^^^^^

Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/traits/issue-65673.stderr
Original file line number Diff line number Diff line change
@@ -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<T> = where T: Trait;
| -------------------------------- this alias does not contain a trait
...
LL | type Ctx = dyn Alias<T>;
| ^^^^^^^^^^^^

Expand Down

0 comments on commit e947fad

Please sign in to comment.