Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't suggest dereferencing to unsized type #115629

Merged
merged 3 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1239,21 +1239,18 @@ pub trait PrettyPrinter<'tcx>:
.generics_of(principal.def_id)
.own_args_no_defaults(cx.tcx(), principal.args);

let mut projections = predicates.projection_bounds();

let mut args = args.iter().cloned();
let arg0 = args.next();
let projection0 = projections.next();
if arg0.is_some() || projection0.is_some() {
let args = arg0.into_iter().chain(args);
let projections = projection0.into_iter().chain(projections);
let mut projections: Vec<_> = predicates.projection_bounds().collect();
projections.sort_by_cached_key(|proj| {
cx.tcx().item_name(proj.item_def_id()).to_string()
});
Comment on lines +1243 to +1245
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly, this isn't necessary for the pretty-printing of impl Trait, because dyn Trait is the only thing that we actually end up sorting the bounds after the fact 🤦‍♂️


if !args.is_empty() || !projections.is_empty() {
p!(generic_delimiters(|mut cx| {
cx = cx.comma_sep(args)?;
if arg0.is_some() && projection0.is_some() {
cx = cx.comma_sep(args.iter().copied())?;
if !args.is_empty() && !projections.is_empty() {
write!(cx, ", ")?;
}
cx.comma_sep(projections)
cx.comma_sep(projections.iter().copied())
}));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,20 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
obligation.param_env,
real_trait_pred_and_base_ty,
);
if self.predicate_may_hold(&obligation) {
let sized_obligation = Obligation::new(
self.tcx,
obligation.cause.clone(),
obligation.param_env,
ty::TraitRef::from_lang_item(
self.tcx,
hir::LangItem::Sized,
obligation.cause.span,
[base_ty],
),
);
if self.predicate_may_hold(&obligation)
&& self.predicate_must_hold_modulo_regions(&sized_obligation)
{
let call_node = self.tcx.hir().get(*call_hir_id);
let msg = "consider dereferencing here";
let is_receiver = matches!(
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/traits/object/enforce-supertrait-projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ trait Trait: SuperTrait<A = <Self as SuperTrait>::B> {}

fn transmute<A, B>(x: A) -> B {
foo::<A, B, dyn Trait<A = A, B = B>>(x)
//~^ ERROR type mismatch resolving `<dyn Trait<B = B, A = A> as SuperTrait>::A == B`
//~^ ERROR type mismatch resolving `<dyn Trait<A = A, B = B> as SuperTrait>::A == B`
}

fn foo<A, B, T: ?Sized>(x: T::A) -> B
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0271]: type mismatch resolving `<dyn Trait<B = B, A = A> as SuperTrait>::A == B`
error[E0271]: type mismatch resolving `<dyn Trait<A = A, B = B> as SuperTrait>::A == B`
--> $DIR/enforce-supertrait-projection.rs:9:17
|
LL | fn transmute<A, B>(x: A) -> B {
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/traits/suggest-dereferences/dont-suggest-unsize-deref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
fn use_iterator<I>(itr: I)
where
I: IntoIterator<Item = i32>,
{
}

fn pass_iterator<I>(i: &dyn IntoIterator<Item = i32, IntoIter = I>)
where
I: Iterator<Item = i32>,
{
use_iterator(i);
//~^ ERROR `&dyn IntoIterator<IntoIter = I, Item = i32>` is not an iterator
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error[E0277]: `&dyn IntoIterator<IntoIter = I, Item = i32>` is not an iterator
--> $DIR/dont-suggest-unsize-deref.rs:11:18
|
LL | use_iterator(i);
| ------------ ^ `&dyn IntoIterator<IntoIter = I, Item = i32>` is not an iterator
| |
| required by a bound introduced by this call
|
= help: the trait `Iterator` is not implemented for `&dyn IntoIterator<IntoIter = I, Item = i32>`
= note: required for `&dyn IntoIterator<IntoIter = I, Item = i32>` to implement `IntoIterator`
note: required by a bound in `use_iterator`
--> $DIR/dont-suggest-unsize-deref.rs:3:8
|
LL | fn use_iterator<I>(itr: I)
| ------------ required by a bound in this function
LL | where
LL | I: IntoIterator<Item = i32>,
| ^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `use_iterator`

error: aborting due to previous error

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