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

Arbitrary self types v2: avoid dyn, generic ICE #136195

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 8 additions & 3 deletions compiler/rustc_middle/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,9 @@ pub enum DynCompatibilityViolation {

/// GAT
GAT(Symbol, Span),

/// Type layout can't be determined
TooGeneric(Span),
}

impl DynCompatibilityViolation {
Expand Down Expand Up @@ -853,16 +856,18 @@ impl DynCompatibilityViolation {
DynCompatibilityViolation::GAT(name, _) => {
format!("it contains the generic associated type `{name}`").into()
}
DynCompatibilityViolation::TooGeneric(_span) => {
format!("it is too generic to determine type layout").into()
}
}
}

pub fn solution(&self) -> DynCompatibilityViolationSolution {
match self {
DynCompatibilityViolation::SizedSelf(_)
| DynCompatibilityViolation::SupertraitSelf(_)
| DynCompatibilityViolation::SupertraitNonLifetimeBinder(..) => {
DynCompatibilityViolationSolution::None
}
| DynCompatibilityViolation::SupertraitNonLifetimeBinder(..)
| DynCompatibilityViolation::TooGeneric(..) => DynCompatibilityViolationSolution::None,
DynCompatibilityViolation::Method(
name,
MethodViolationCode::StaticMethod(Some((add_self_sugg, make_sized_sugg))),
Expand Down
18 changes: 16 additions & 2 deletions compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use super::elaborate;
use crate::infer::TyCtxtInferExt;
pub use crate::traits::DynCompatibilityViolation;
use crate::traits::query::evaluate_obligation::InferCtxtExt;
use crate::traits::ty::layout::LayoutError;
use crate::traits::{MethodViolationCode, Obligation, ObligationCause, util};

/// Returns the dyn-compatibility violations that affect HIR ty lowering.
Expand Down Expand Up @@ -112,7 +113,7 @@ fn dyn_compatibility_violations_for_trait(
if violations.is_empty() {
for item in tcx.associated_items(trait_def_id).in_definition_order() {
if let ty::AssocKind::Fn = item.kind {
check_receiver_correct(tcx, trait_def_id, *item);
check_receiver_correct(tcx, trait_def_id, *item, &mut violations);
}
}
}
Expand Down Expand Up @@ -501,9 +502,16 @@ fn virtual_call_violations_for_method<'tcx>(

/// This code checks that `receiver_is_dispatchable` is correctly implemented.
///
/// Any errors are appended to `violations`.
///
/// This check is outlined from the dyn-compatibility check to avoid cycles with
/// layout computation, which relies on knowing whether methods are dyn-compatible.
fn check_receiver_correct<'tcx>(tcx: TyCtxt<'tcx>, trait_def_id: DefId, method: ty::AssocItem) {
fn check_receiver_correct<'tcx>(
tcx: TyCtxt<'tcx>,
trait_def_id: DefId,
method: ty::AssocItem,
violations: &mut Vec<DynCompatibilityViolation>,
) {
if !is_vtable_safe_method(tcx, trait_def_id, method) {
return;
}
Expand All @@ -522,6 +530,9 @@ fn check_receiver_correct<'tcx>(tcx: TyCtxt<'tcx>, trait_def_id: DefId, method:
let unit_receiver_ty = receiver_for_self_ty(tcx, receiver_ty, tcx.types.unit, method_def_id);
match tcx.layout_of(typing_env.as_query_input(unit_receiver_ty)).map(|l| l.backend_repr) {
Ok(BackendRepr::Scalar(..)) => (),
Err(LayoutError::TooGeneric(..)) => {
violations.push(DynCompatibilityViolation::TooGeneric(tcx.def_span(method_def_id)));
}
abi => {
tcx.dcx().span_delayed_bug(
tcx.def_span(method_def_id),
Expand All @@ -537,6 +548,9 @@ fn check_receiver_correct<'tcx>(tcx: TyCtxt<'tcx>, trait_def_id: DefId, method:
receiver_for_self_ty(tcx, receiver_ty, trait_object_ty, method_def_id);
match tcx.layout_of(typing_env.as_query_input(trait_object_receiver)).map(|l| l.backend_repr) {
Ok(BackendRepr::ScalarPair(..)) => (),
Err(LayoutError::TooGeneric(..)) => {
violations.push(DynCompatibilityViolation::TooGeneric(tcx.def_span(method_def_id)));
}
abi => {
tcx.dcx().span_delayed_bug(
tcx.def_span(method_def_id),
Expand Down
10 changes: 0 additions & 10 deletions tests/crashes/125810.rs

This file was deleted.

11 changes: 0 additions & 11 deletions tests/crashes/57276.rs

This file was deleted.

11 changes: 11 additions & 0 deletions tests/ui/self/dispatch-from-dyn-layout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![feature(arbitrary_self_types, dispatch_from_dyn)]

use std::ops::{Deref, DispatchFromDyn};

trait Trait<T: Deref<Target=Self> + DispatchFromDyn<T>> {
fn foo(self: T) -> dyn Trait<T>;
//~^ ERROR: associated item referring to unboxed trait object for its own trait
//~| ERROR: the trait `Trait` is not dyn compatible
}

fn main() {}
26 changes: 26 additions & 0 deletions tests/ui/self/dispatch-from-dyn-layout.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
error: associated item referring to unboxed trait object for its own trait
--> $DIR/dispatch-from-dyn-layout.rs:6:24
|
LL | trait Trait<T: Deref<Target=Self> + DispatchFromDyn<T>> {
| ----- in this trait
LL | fn foo(self: T) -> dyn Trait<T>;
| ^^^^^^^^^^^^
|
help: you might have meant to use `Self` to refer to the implementing type
|
LL | fn foo(self: T) -> Self;
| ~~~~

error[E0038]: the trait `Trait` is not dyn compatible
--> $DIR/dispatch-from-dyn-layout.rs:6:24
|
LL | fn foo(self: T) -> dyn Trait<T>;
| ^^^^^^^^^^^^ `Trait` is not dyn compatible
|
= note: the trait is not dyn compatible because it is too generic to determine type layout
= note: for a trait to be dyn compatible it needs to allow building a vtable
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>

error: aborting due to 2 previous errors

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