Skip to content

Commit ee9727e

Browse files
Don't suggest dereferencing to unsized type
1 parent f00c139 commit ee9727e

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,20 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
838838
obligation.param_env,
839839
real_trait_pred_and_base_ty,
840840
);
841-
if self.predicate_may_hold(&obligation) {
841+
let sized_obligation = Obligation::new(
842+
self.tcx,
843+
obligation.cause.clone(),
844+
obligation.param_env,
845+
ty::TraitRef::from_lang_item(
846+
self.tcx,
847+
hir::LangItem::Sized,
848+
obligation.cause.span,
849+
[base_ty],
850+
),
851+
);
852+
if self.predicate_may_hold(&obligation)
853+
&& self.predicate_must_hold_modulo_regions(&sized_obligation)
854+
{
842855
let call_node = self.tcx.hir().get(*call_hir_id);
843856
let msg = "consider dereferencing here";
844857
let is_receiver = matches!(
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
fn use_iterator<I>(itr: I)
2+
where
3+
I: IntoIterator<Item = i32>,
4+
{
5+
}
6+
7+
fn pass_iterator<I>(i: &dyn IntoIterator<Item = i32, IntoIter = I>)
8+
where
9+
I: Iterator<Item = i32>,
10+
{
11+
use_iterator(i);
12+
//~^ ERROR `&dyn IntoIterator<Item = i32, IntoIter = I>` is not an iterator
13+
}
14+
15+
fn main() {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0277]: `&dyn IntoIterator<Item = i32, IntoIter = I>` is not an iterator
2+
--> $DIR/dont-suggest-unsize-deref.rs:11:18
3+
|
4+
LL | use_iterator(i);
5+
| ------------ ^ `&dyn IntoIterator<Item = i32, IntoIter = I>` is not an iterator
6+
| |
7+
| required by a bound introduced by this call
8+
|
9+
= help: the trait `Iterator` is not implemented for `&dyn IntoIterator<Item = i32, IntoIter = I>`
10+
= note: required for `&dyn IntoIterator<Item = i32, IntoIter = I>` to implement `IntoIterator`
11+
note: required by a bound in `use_iterator`
12+
--> $DIR/dont-suggest-unsize-deref.rs:3:8
13+
|
14+
LL | fn use_iterator<I>(itr: I)
15+
| ------------ required by a bound in this function
16+
LL | where
17+
LL | I: IntoIterator<Item = i32>,
18+
| ^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `use_iterator`
19+
20+
error: aborting due to previous error
21+
22+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)