From e7fdb2ea320d856079fe99c86e6791692b4c2d59 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 18 Nov 2024 23:24:05 +0000 Subject: [PATCH] Don't register predicates for RPITIT items in built-in trait object impl confirmation --- .../src/traits/select/confirmation.rs | 14 ++++++-- compiler/rustc_ty_utils/src/abi.rs | 2 +- tests/crashes/131538.rs | 13 ------- .../dyn/gat-extended-bad-afit.no.stderr | 30 ++++++++++++++++ .../async-await/dyn/gat-extended-bad-afit.rs | 24 +++++++++++++ .../dyn/gat-extended-bad-afit.yes.stderr | 35 +++++++++++++++++++ 6 files changed, 102 insertions(+), 16 deletions(-) delete mode 100644 tests/crashes/131538.rs create mode 100644 tests/ui/async-await/dyn/gat-extended-bad-afit.no.stderr create mode 100644 tests/ui/async-await/dyn/gat-extended-bad-afit.rs create mode 100644 tests/ui/async-await/dyn/gat-extended-bad-afit.yes.stderr diff --git a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs index 93183aa90f56d..64140d7a9452e 100644 --- a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs +++ b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs @@ -629,8 +629,18 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { // When `async_fn_in_dyn_trait` is enabled, we don't need to check the // RPITIT for compatibility, since it's not provided by the user. - if tcx.features().async_fn_in_dyn_trait() && tcx.is_impl_trait_in_trait(assoc_type) { - continue; + if tcx.is_impl_trait_in_trait(assoc_type) { + if tcx.features().async_fn_in_dyn_trait() { + continue; + } else { + tcx.dcx().span_delayed_bug( + obligation.cause.span, + "RPITIT in trait object shouldn't have been considered", + ); + return Err(SelectionError::TraitDynIncompatible( + trait_predicate.trait_ref.def_id, + )); + } } if !defs.own_params.is_empty() && !tcx.features().generic_associated_types_extended() { diff --git a/compiler/rustc_ty_utils/src/abi.rs b/compiler/rustc_ty_utils/src/abi.rs index d286ffbc4a613..c958828300b22 100644 --- a/compiler/rustc_ty_utils/src/abi.rs +++ b/compiler/rustc_ty_utils/src/abi.rs @@ -324,7 +324,7 @@ fn fn_abi_of_fn_ptr<'tcx>( ) -> Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, &'tcx FnAbiError<'tcx>> { let ty::PseudoCanonicalInput { typing_env, value: (sig, extra_args) } = query; - let cx = LayoutCx::new(tcx, param_env); + let cx = LayoutCx::new(tcx, typing_env); fn_abi_new_uncached( &cx, tcx.instantiate_bound_regions_with_erased(sig), diff --git a/tests/crashes/131538.rs b/tests/crashes/131538.rs deleted file mode 100644 index f971d8b7791e4..0000000000000 --- a/tests/crashes/131538.rs +++ /dev/null @@ -1,13 +0,0 @@ -//@ known-bug: #131538 -#![feature(generic_associated_types_extended)] -#![feature(trivial_bounds)] - -trait HealthCheck { - async fn check(); -} - -fn do_health_check_par() -where - HealthCheck: HealthCheck, -{ -} diff --git a/tests/ui/async-await/dyn/gat-extended-bad-afit.no.stderr b/tests/ui/async-await/dyn/gat-extended-bad-afit.no.stderr new file mode 100644 index 0000000000000..036955d06771a --- /dev/null +++ b/tests/ui/async-await/dyn/gat-extended-bad-afit.no.stderr @@ -0,0 +1,30 @@ +warning: the feature `generic_associated_types_extended` is incomplete and may not be safe to use and/or cause compiler crashes + --> $DIR/gat-extended-bad-afit.rs:9:12 + | +LL | #![feature(generic_associated_types_extended)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #95451 for more information + = note: `#[warn(incomplete_features)]` on by default + +error[E0038]: the trait `HealthCheck` cannot be made into an object + --> $DIR/gat-extended-bad-afit.rs:19:22 + | +LL | dyn HealthCheck: HealthCheck, + | ^^^^^^^^^^^ `HealthCheck` cannot be made into an object + | +note: for a trait to be "dyn-compatible" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit + --> $DIR/gat-extended-bad-afit.rs:14:14 + | +LL | trait HealthCheck { + | ----------- this trait cannot be made into an object... +LL | async fn check(&self); + | ^^^^^ + | | + | ...because method `check` is `async` + | ...because method `check` has generic type parameters + = help: consider moving `check` to another trait + +error: aborting due to 1 previous error; 1 warning emitted + +For more information about this error, try `rustc --explain E0038`. diff --git a/tests/ui/async-await/dyn/gat-extended-bad-afit.rs b/tests/ui/async-await/dyn/gat-extended-bad-afit.rs new file mode 100644 index 0000000000000..05b14efc312eb --- /dev/null +++ b/tests/ui/async-await/dyn/gat-extended-bad-afit.rs @@ -0,0 +1,24 @@ +//@ edition: 2021 +//@ revisions: yes no + +// Ice regression test for #131538 +// Make sure that we don't crash when the GATE feature gate is enabled. + +#![cfg_attr(yes, feature(async_fn_in_dyn_trait))] +//[yes]~^ WARN the feature `async_fn_in_dyn_trait` is incomplete +#![feature(generic_associated_types_extended)] +//~^ WARN the feature `generic_associated_types_extended` is incomplete +#![feature(trivial_bounds)] + +trait HealthCheck { + async fn check(&self); +} + +fn do_health_check_par() +where + dyn HealthCheck: HealthCheck, + //~^ ERROR the trait `HealthCheck` cannot be made into an object +{ +} + +fn main() {} diff --git a/tests/ui/async-await/dyn/gat-extended-bad-afit.yes.stderr b/tests/ui/async-await/dyn/gat-extended-bad-afit.yes.stderr new file mode 100644 index 0000000000000..7c59b5ae84eca --- /dev/null +++ b/tests/ui/async-await/dyn/gat-extended-bad-afit.yes.stderr @@ -0,0 +1,35 @@ +warning: the feature `async_fn_in_dyn_trait` is incomplete and may not be safe to use and/or cause compiler crashes + --> $DIR/gat-extended-bad-afit.rs:7:26 + | +LL | #![cfg_attr(yes, feature(async_fn_in_dyn_trait))] + | ^^^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #133119 for more information + = note: `#[warn(incomplete_features)]` on by default + +warning: the feature `generic_associated_types_extended` is incomplete and may not be safe to use and/or cause compiler crashes + --> $DIR/gat-extended-bad-afit.rs:9:12 + | +LL | #![feature(generic_associated_types_extended)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #95451 for more information + +error[E0038]: the trait `HealthCheck` cannot be made into an object + --> $DIR/gat-extended-bad-afit.rs:19:22 + | +LL | dyn HealthCheck: HealthCheck, + | ^^^^^^^^^^^ `HealthCheck` cannot be made into an object + | +note: for a trait to be "dyn-compatible" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit + --> $DIR/gat-extended-bad-afit.rs:14:14 + | +LL | trait HealthCheck { + | ----------- this trait cannot be made into an object... +LL | async fn check(&self); + | ^^^^^ ...because method `check` has generic type parameters + = help: consider moving `check` to another trait + +error: aborting due to 1 previous error; 2 warnings emitted + +For more information about this error, try `rustc --explain E0038`.