Skip to content

Commit

Permalink
Detect cases when user written object assoc bound differs from elabor…
Browse files Browse the repository at this point in the history
…ated non-self-referential bound
  • Loading branch information
compiler-errors committed Nov 23, 2024
1 parent f5be3ca commit 4d483f5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
use rustc_errors::codes::*;
use rustc_errors::struct_span_code_err;
use rustc_hir as hir;
Expand Down Expand Up @@ -62,6 +62,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {

let mut trait_bounds = vec![];
let mut projection_bounds = vec![];
let mut uwu = FxHashMap::default();

for (pred, span) in bounds.clauses() {
let bound_pred = pred.kind();
match bound_pred.skip_binder() {
Expand All @@ -71,6 +73,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
}
ty::ClauseKind::Projection(proj) => {
projection_bounds.push((bound_pred.rebind(proj), span));
uwu.insert(proj.def_id(), (bound_pred.rebind(proj.term), span));
}
ty::ClauseKind::TypeOutlives(_) => {
// Do nothing, we deal with regions separately
Expand Down Expand Up @@ -176,6 +179,20 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
projection_bounds.push((pred, original_span));
}

if !references_self
&& let Some(&(user_written, span)) = uwu.get(&pred.projection_def_id())
{
let user_written = tcx.anonymize_bound_vars(user_written);
let elaborated =
tcx.anonymize_bound_vars(pred.map_bound(|pred| pred.term));
if user_written != elaborated {
self.dcx().span_err(
span,
format!("expected {user_written}, found {elaborated}"),
);
}
}

self.check_elaborated_projection_mentions_input_lifetimes(
pred,
original_span,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
error: expected u32, found i32
--> $DIR/associated-types-overridden-binding-2.rs:6:29
|
LL | let _: &dyn I32Iterator<Item = u32> = &vec![42].into_iter();
| ^^^^^^^^^^

error[E0271]: expected `IntoIter<u32>` to be an iterator that yields `i32`, but it yields `u32`
--> $DIR/associated-types-overridden-binding-2.rs:6:43
|
Expand All @@ -6,6 +12,6 @@ LL | let _: &dyn I32Iterator<Item = u32> = &vec![42].into_iter();
|
= note: required for the cast from `&std::vec::IntoIter<u32>` to `&dyn Iterator<Item = u32, Item = i32>`

error: aborting due to 1 previous error
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0271`.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ note: required by a bound in `I32Iterator`
LL | trait I32Iterator = Iterator<Item = i32>;
| ^^^^^^^^^^ required by this bound in `I32Iterator`

error: aborting due to 2 previous errors
error: expected u32, found i32
--> $DIR/associated-types-overridden-binding.rs:10:29
|
LL | let _: &dyn I32Iterator<Item = u32>;
| ^^^^^^^^^^

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0284`.
14 changes: 13 additions & 1 deletion tests/ui/traits/object/pretty.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
error: expected u16, found u8
--> $DIR/pretty.rs:28:34
|
LL | fn dyn_fixed_multi(x: &dyn Fixed<Assoc = u16>) { x }
| ^^^^^^^^^^^

error: expected &'a u8, found for<'a> &'a u8
--> $DIR/pretty.rs:36:62
|
LL | fn dyn_fixed_generic_multi(x: &dyn for<'a> FixedGeneric1<'a, Assoc2 = &u8>) { x }
| ^^^^^^^^^^^^

warning: unnecessary associated type bound for dyn-incompatible associated type
--> $DIR/pretty.rs:41:35
|
Expand Down Expand Up @@ -161,6 +173,6 @@ LL | fn dyn_has_gat(x: &dyn HasGat<u8, Assoc<bool> = ()>) { x }
= note: expected unit type `()`
found reference `&dyn HasGat<u8, Assoc<bool> = ()>`

error: aborting due to 14 previous errors; 1 warning emitted
error: aborting due to 16 previous errors; 1 warning emitted

For more information about this error, try `rustc --explain E0308`.

0 comments on commit 4d483f5

Please sign in to comment.