Skip to content

Commit 19175e9

Browse files
Synthesize generics for bad auto traits in dyn types
1 parent d144956 commit 19175e9

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -722,8 +722,28 @@ impl<'tcx> PolyExistentialPredicate<'tcx> {
722722
self.rebind(p.with_self_ty(tcx, self_ty)).to_predicate(tcx)
723723
}
724724
ExistentialPredicate::AutoTrait(did) => {
725-
let trait_ref = self.rebind(tcx.mk_trait_ref(did, [self_ty]));
726-
trait_ref.without_const().to_predicate(tcx)
725+
let generics = tcx.generics_of(did);
726+
let trait_ref = if generics.params.len() == 1 {
727+
tcx.mk_trait_ref(did, [self_ty])
728+
} else {
729+
// If this is an ill-formed auto trait, then synthesize
730+
// new error substs for the missing generics.
731+
let err_substs = ty::InternalSubsts::for_item(tcx, did, |def, substs| {
732+
if def.index == 0 {
733+
self_ty.into()
734+
} else {
735+
match &def.kind {
736+
ty::GenericParamDefKind::Lifetime => tcx.lifetimes.re_static.into(),
737+
ty::GenericParamDefKind::Type { .. } => tcx.ty_error().into(),
738+
ty::GenericParamDefKind::Const { .. } => tcx
739+
.const_error(tcx.bound_type_of(def.def_id).subst(tcx, substs))
740+
.into(),
741+
}
742+
}
743+
});
744+
tcx.mk_trait_ref(did, err_substs)
745+
};
746+
self.rebind(trait_ref).without_const().to_predicate(tcx)
727747
}
728748
}
729749
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(auto_traits)]
2+
3+
auto trait Trait1<'a> {}
4+
//~^ ERROR auto traits cannot have generic parameters
5+
6+
fn f<'a>(x: &dyn Trait1<'a>)
7+
{}
8+
9+
fn main() {
10+
f(&1);
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0567]: auto traits cannot have generic parameters
2+
--> $DIR/bad-generics-on-dyn.rs:3:18
3+
|
4+
LL | auto trait Trait1<'a> {}
5+
| ------^^^^ help: remove the parameters
6+
| |
7+
| auto trait cannot have generic parameters
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0567`.

0 commit comments

Comments
 (0)