Validate associated type bounds on ?

This commit is contained in:
Michael Goulet 2024-10-31 00:09:34 +00:00
parent e356279bdf
commit 06a49b609f
3 changed files with 58 additions and 13 deletions

View File

@ -681,11 +681,32 @@ pub(crate) fn lower_poly_trait_ref(
Some(self_ty),
);
let tcx = self.tcx();
let bound_vars = tcx.late_bound_vars(trait_ref.hir_ref_id);
debug!(?bound_vars);
let poly_trait_ref = ty::Binder::bind_with_vars(
ty::TraitRef::new_from_args(tcx, trait_def_id, generic_args),
bound_vars,
);
let polarity = match polarity {
rustc_ast::BoundPolarity::Positive => ty::PredicatePolarity::Positive,
rustc_ast::BoundPolarity::Negative(_) => ty::PredicatePolarity::Negative,
rustc_ast::BoundPolarity::Maybe(_) => {
// No-op.
// Validate associated type at least. We may want to reject these
// outright in the future...
for constraint in trait_segment.args().constraints {
let _ = self.lower_assoc_item_constraint(
trait_ref.hir_ref_id,
poly_trait_ref,
constraint,
&mut Default::default(),
&mut Default::default(),
constraint.span,
predicate_filter,
);
}
return arg_count;
}
};
@ -699,15 +720,6 @@ pub(crate) fn lower_poly_trait_ref(
});
}
let tcx = self.tcx();
let bound_vars = tcx.late_bound_vars(trait_ref.hir_ref_id);
debug!(?bound_vars);
let poly_trait_ref = ty::Binder::bind_with_vars(
ty::TraitRef::new_from_args(tcx, trait_def_id, generic_args),
bound_vars,
);
match predicate_filter {
PredicateFilter::All
| PredicateFilter::SelfOnly
@ -758,11 +770,11 @@ pub(crate) fn lower_poly_trait_ref(
// since we should have emitted an error for them earlier, and they
// would not be well-formed!
if polarity != ty::PredicatePolarity::Positive {
assert!(
self.dcx().has_errors().is_some(),
self.dcx().span_delayed_bug(
constraint.span,
"negative trait bounds should not have assoc item constraints",
);
continue;
break;
}
// Specify type to assert that error was already reported in `Err` case.

View File

@ -0,0 +1,12 @@
trait HasAssoc {
type Assoc;
}
fn hasassoc<T: ?HasAssoc<Assoc = ()>>() {}
//~^ WARN relaxing a default bound
trait NoAssoc {}
fn noassoc<T: ?NoAssoc<Missing = ()>>() {}
//~^ WARN relaxing a default bound
//~| ERROR associated type `Missing` not found for `NoAssoc`
fn main() {}

View File

@ -0,0 +1,21 @@
warning: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
--> $DIR/maybe-bound-with-assoc.rs:4:16
|
LL | fn hasassoc<T: ?HasAssoc<Assoc = ()>>() {}
| ^^^^^^^^^^^^^^^^^^^^^
warning: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
--> $DIR/maybe-bound-with-assoc.rs:8:15
|
LL | fn noassoc<T: ?NoAssoc<Missing = ()>>() {}
| ^^^^^^^^^^^^^^^^^^^^^^
error[E0220]: associated type `Missing` not found for `NoAssoc`
--> $DIR/maybe-bound-with-assoc.rs:8:24
|
LL | fn noassoc<T: ?NoAssoc<Missing = ()>>() {}
| ^^^^^^^ associated type `Missing` not found
error: aborting due to 1 previous error; 2 warnings emitted
For more information about this error, try `rustc --explain E0220`.