Skip to content

Commit 208d6db

Browse files
Rollup merge of #165851 - amirHdev:objectpick-invalid-receiver-recovery, r=mejrs
fix ICE when confirming invalid dyn method receivers fixes #165033 method lookup can pick a method with an invalid receiver which causes an ICE during confirmation and if the method has already failed well-formedness checking we recover instead of crashing
2 parents 60d0191 + 62368a0 commit 208d6db

3 files changed

Lines changed: 71 additions & 27 deletions

File tree

‎compiler/rustc_hir_typeck/src/method/confirm.rs‎

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -333,27 +333,32 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
333333
return ty::GenericArgs::extend_with_error(self.tcx, trait_def_id, &[]);
334334
}
335335

336-
self.extract_existential_trait_ref(self_ty, |this, object_ty, principal| {
337-
// The object data has no entry for the Self
338-
// Type. For the purposes of this method call, we
339-
// instantiate the object type itself. This
340-
// wouldn't be a sound instantiation in all cases,
341-
// since each instance of the object type is a
342-
// different existential and hence could match
343-
// distinct types (e.g., if `Self` appeared as an
344-
// argument type), but those cases have already
345-
// been ruled out when we deemed the trait to be
346-
// "dyn-compatible".
347-
let original_poly_trait_ref = principal.with_self_ty(this.tcx, object_ty);
348-
let upcast_poly_trait_ref = this.upcast(original_poly_trait_ref, trait_def_id);
349-
let upcast_trait_ref =
350-
this.instantiate_binder_with_fresh_vars(upcast_poly_trait_ref);
351-
debug!(
352-
"original_poly_trait_ref={:?} upcast_trait_ref={:?} target_trait={:?}",
353-
original_poly_trait_ref, upcast_trait_ref, trait_def_id
354-
);
355-
upcast_trait_ref.args
356-
})
336+
self.extract_existential_trait_ref(
337+
self_ty,
338+
trait_def_id,
339+
|this, object_ty, principal| {
340+
// The object data has no entry for the Self
341+
// Type. For the purposes of this method call, we
342+
// instantiate the object type itself. This
343+
// wouldn't be a sound instantiation in all cases,
344+
// since each instance of the object type is a
345+
// different existential and hence could match
346+
// distinct types (e.g., if `Self` appeared as an
347+
// argument type), but those cases have already
348+
// been ruled out when we deemed the trait to be
349+
// "dyn-compatible".
350+
let original_poly_trait_ref = principal.with_self_ty(this.tcx, object_ty);
351+
let upcast_poly_trait_ref =
352+
this.upcast(original_poly_trait_ref, trait_def_id);
353+
let upcast_trait_ref =
354+
this.instantiate_binder_with_fresh_vars(upcast_poly_trait_ref);
355+
debug!(
356+
"original_poly_trait_ref={:?} upcast_trait_ref={:?} target_trait={:?}",
357+
original_poly_trait_ref, upcast_trait_ref, trait_def_id
358+
);
359+
upcast_trait_ref.args
360+
},
361+
)
357362
}
358363

359364
probe::TraitPick { .. } => {
@@ -375,9 +380,18 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
375380
}
376381
}
377382

378-
fn extract_existential_trait_ref<R, F>(&mut self, self_ty: Ty<'tcx>, mut closure: F) -> R
383+
fn extract_existential_trait_ref<F>(
384+
&mut self,
385+
self_ty: Ty<'tcx>,
386+
trait_def_id: DefId,
387+
mut closure: F,
388+
) -> GenericArgs<'tcx>
379389
where
380-
F: FnMut(&mut ConfirmContext<'a, 'tcx>, Ty<'tcx>, ty::PolyExistentialTraitRef<'tcx>) -> R,
390+
F: FnMut(
391+
&mut ConfirmContext<'a, 'tcx>,
392+
Ty<'tcx>,
393+
ty::PolyExistentialTraitRef<'tcx>,
394+
) -> GenericArgs<'tcx>,
381395
{
382396
// If we specified that this is an object method, then the
383397
// self-type ought to be something that can be dereferenced to
@@ -407,11 +421,11 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
407421
_ => None,
408422
})
409423
.unwrap_or_else(|| {
410-
span_bug!(
424+
self.dcx().span_delayed_bug(
411425
self.span,
412-
"self-type `{}` for ObjectPick never dereferenced to an object",
413-
self_ty
414-
)
426+
format!("self-type `{self_ty}` for ObjectPick never dereferenced to an object"),
427+
);
428+
ty::GenericArgs::extend_with_error(self.tcx, trait_def_id, &[])
415429
})
416430
}
417431

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Regression test for rust-lang/rust#165033.
2+
// An invalid receiver must not trigger an ICE during method confirmation.
3+
4+
use std::cell::Cell;
5+
use std::ops::Deref;
6+
7+
trait Foo: Deref<Target = Cell<dyn Bar>> {
8+
fn method(self: &Cell<dyn Bar>) {}
9+
//~^ ERROR invalid `self` parameter type
10+
}
11+
12+
trait Bar {}
13+
14+
fn test(x: &dyn Foo) {
15+
x.method();
16+
}
17+
18+
fn main() {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0307]: invalid `self` parameter type: `&Cell<(dyn Bar + 'static)>`
2+
--> $DIR/invalid-self-dyn-receiver-cell.rs:8:21
3+
|
4+
LL | fn method(self: &Cell<dyn Bar>) {}
5+
| ^^^^^^^^^^^^^^
6+
|
7+
= note: type of `self` must be `Self` or a type that dereferences to it
8+
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
9+
10+
error: aborting due to 1 previous error
11+
12+
For more information about this error, try `rustc --explain E0307`.

0 commit comments

Comments
 (0)