Skip to content

Commit 28b566d

Browse files
oli-obkMabezDev
authored andcommitted
Revert "Auto merge of rust-lang#118133 - Urgau:stabilize_trait_upcasting, r=WaffleLapkin"
This reverts commit 6d2b84b, reversing changes made to 73bc121.
1 parent 6416fb7 commit 28b566d

File tree

71 files changed

+353
-105
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+353
-105
lines changed

compiler/rustc_feature/src/accepted.rs

-3
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,6 @@ declare_features! (
337337
/// Allows `#[track_caller]` to be used which provides
338338
/// accurate caller location reporting during panic (RFC 2091).
339339
(accepted, track_caller, "1.46.0", Some(47809)),
340-
/// Allows dyn upcasting trait objects via supertraits.
341-
/// Dyn upcasting is casting, e.g., `dyn Foo -> dyn Bar` where `Foo: Bar`.
342-
(accepted, trait_upcasting, "1.76.0", Some(65991)),
343340
/// Allows #[repr(transparent)] on univariant enums (RFC 2645).
344341
(accepted, transparent_enums, "1.42.0", Some(60405)),
345342
/// Allows indexing tuples.

compiler/rustc_feature/src/unstable.rs

+3
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,9 @@ declare_features! (
580580
(unstable, thread_local, "1.0.0", Some(29594)),
581581
/// Allows defining `trait X = A + B;` alias items.
582582
(unstable, trait_alias, "1.24.0", Some(41517)),
583+
/// Allows dyn upcasting trait objects via supertraits.
584+
/// Dyn upcasting is casting, e.g., `dyn Foo -> dyn Bar` where `Foo: Bar`.
585+
(unstable, trait_upcasting, "1.56.0", Some(65991)),
583586
/// Allows for transmuting between arrays with sizes that contain generic consts.
584587
(unstable, transmute_generic_consts, "1.70.0", Some(109929)),
585588
/// Allows #[repr(transparent)] on unions (RFC 2645).

compiler/rustc_hir_typeck/src/coercion.rs

+23
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
627627
)];
628628

629629
let mut has_unsized_tuple_coercion = false;
630+
let mut has_trait_upcasting_coercion = None;
630631

631632
// Keep resolving `CoerceUnsized` and `Unsize` predicates to avoid
632633
// emitting a coercion in cases like `Foo<$1>` -> `Foo<$2>`, where
@@ -694,6 +695,13 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
694695
// these here and emit a feature error if coercion doesn't fail
695696
// due to another reason.
696697
match impl_source {
698+
traits::ImplSource::Builtin(
699+
BuiltinImplSource::TraitUpcasting { .. },
700+
_,
701+
) => {
702+
has_trait_upcasting_coercion =
703+
Some((trait_pred.self_ty(), trait_pred.trait_ref.args.type_at(1)));
704+
}
697705
traits::ImplSource::Builtin(BuiltinImplSource::TupleUnsizing, _) => {
698706
has_unsized_tuple_coercion = true;
699707
}
@@ -704,6 +712,21 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
704712
}
705713
}
706714

715+
if let Some((sub, sup)) = has_trait_upcasting_coercion
716+
&& !self.tcx().features().trait_upcasting
717+
{
718+
// Renders better when we erase regions, since they're not really the point here.
719+
let (sub, sup) = self.tcx.erase_regions((sub, sup));
720+
let mut err = feature_err(
721+
&self.tcx.sess.parse_sess,
722+
sym::trait_upcasting,
723+
self.cause.span,
724+
format!("cannot cast `{sub}` to `{sup}`, trait upcasting coercion is experimental"),
725+
);
726+
err.note(format!("required when coercing `{source}` into `{target}`"));
727+
err.emit();
728+
}
729+
707730
if has_unsized_tuple_coercion && !self.tcx.features().unsized_tuple_coercion {
708731
feature_err(
709732
&self.tcx.sess.parse_sess,

compiler/rustc_lint/src/deref_into_dyn_supertrait.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ use crate::{
55

66
use rustc_hir as hir;
77
use rustc_middle::ty;
8+
use rustc_session::lint::FutureIncompatibilityReason;
89
use rustc_span::sym;
910
use rustc_trait_selection::traits::supertraits;
1011

1112
declare_lint! {
1213
/// The `deref_into_dyn_supertrait` lint is output whenever there is a use of the
1314
/// `Deref` implementation with a `dyn SuperTrait` type as `Output`.
1415
///
16+
/// These implementations will become shadowed when the `trait_upcasting` feature is stabilized.
17+
/// The `deref` functions will no longer be called implicitly, so there might be behavior change.
18+
///
1519
/// ### Example
1620
///
1721
/// ```rust,compile_fail
@@ -40,10 +44,15 @@ declare_lint! {
4044
///
4145
/// ### Explanation
4246
///
43-
/// The implicit dyn upcasting coercion take priority over those `Deref` impls.
47+
/// The dyn upcasting coercion feature adds new coercion rules, taking priority
48+
/// over certain other coercion rules, which will cause some behavior change.
4449
pub DEREF_INTO_DYN_SUPERTRAIT,
4550
Warn,
46-
"`Deref` implementation usage with a supertrait trait object for output are shadow by implicit coercion",
51+
"`Deref` implementation usage with a supertrait trait object for output might be shadowed in the future",
52+
@future_incompatible = FutureIncompatibleInfo {
53+
reason: FutureIncompatibilityReason::FutureReleaseSemanticsChange,
54+
reference: "issue #89460 <https://github.com/rust-lang/rust/issues/89460>",
55+
};
4756
}
4857

4958
declare_lint_pass!(DerefIntoDynSupertrait => [DEREF_INTO_DYN_SUPERTRAIT]);

compiler/rustc_lint/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#![feature(min_specialization)]
4040
#![feature(never_type)]
4141
#![feature(rustc_attrs)]
42-
#![cfg_attr(bootstrap, feature(trait_upcasting))]
42+
#![feature(trait_upcasting)]
4343
#![recursion_limit = "256"]
4444
#![deny(rustc::untranslatable_diagnostic)]
4545
#![deny(rustc::diagnostic_outside_of_impl)]

compiler/rustc_lint/src/lints.rs

-1
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,6 @@ pub enum BuiltinSpecialModuleNameUsed {
544544
// deref_into_dyn_supertrait.rs
545545
#[derive(LintDiagnostic)]
546546
#[diag(lint_supertrait_as_deref_target)]
547-
#[help]
548547
pub struct SupertraitAsDerefTarget<'a> {
549548
pub self_ty: Ty<'a>,
550549
pub supertrait_principal: PolyExistentialTraitRef<'a>,

compiler/rustc_middle/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
#![feature(associated_type_bounds)]
5050
#![feature(rustc_attrs)]
5151
#![feature(control_flow_enum)]
52-
#![cfg_attr(bootstrap, feature(trait_upcasting))]
52+
#![feature(trait_upcasting)]
5353
#![feature(trusted_step)]
5454
#![feature(try_blocks)]
5555
#![feature(try_reserve_kind)]

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

+51
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
use hir::def_id::DefId;
1010
use hir::LangItem;
1111
use rustc_hir as hir;
12+
use rustc_infer::traits::ObligationCause;
1213
use rustc_infer::traits::{Obligation, PolyTraitObligation, SelectionError};
1314
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams};
1415
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
1516

17+
use crate::traits;
18+
use crate::traits::query::evaluate_obligation::InferCtxtExt;
1619
use crate::traits::util;
1720

1821
use super::BuiltinImplConditions;
@@ -723,6 +726,45 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
723726
})
724727
}
725728

729+
/// Temporary migration for #89190
730+
fn need_migrate_deref_output_trait_object(
731+
&mut self,
732+
ty: Ty<'tcx>,
733+
param_env: ty::ParamEnv<'tcx>,
734+
cause: &ObligationCause<'tcx>,
735+
) -> Option<ty::PolyExistentialTraitRef<'tcx>> {
736+
let tcx = self.tcx();
737+
if tcx.features().trait_upcasting {
738+
return None;
739+
}
740+
741+
// <ty as Deref>
742+
let trait_ref = ty::TraitRef::new(tcx, tcx.lang_items().deref_trait()?, [ty]);
743+
744+
let obligation =
745+
traits::Obligation::new(tcx, cause.clone(), param_env, ty::Binder::dummy(trait_ref));
746+
if !self.infcx.predicate_may_hold(&obligation) {
747+
return None;
748+
}
749+
750+
self.infcx.probe(|_| {
751+
let ty = traits::normalize_projection_type(
752+
self,
753+
param_env,
754+
ty::AliasTy::new(tcx, tcx.lang_items().deref_target()?, trait_ref.args),
755+
cause.clone(),
756+
0,
757+
// We're *intentionally* throwing these away,
758+
// since we don't actually use them.
759+
&mut vec![],
760+
)
761+
.ty()
762+
.unwrap();
763+
764+
if let ty::Dynamic(data, ..) = ty.kind() { data.principal() } else { None }
765+
})
766+
}
767+
726768
/// Searches for unsizing that might apply to `obligation`.
727769
fn assemble_candidates_for_unsizing(
728770
&mut self,
@@ -780,6 +822,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
780822
let principal_a = a_data.principal().unwrap();
781823
let target_trait_did = principal_def_id_b.unwrap();
782824
let source_trait_ref = principal_a.with_self_ty(self.tcx(), source);
825+
if let Some(deref_trait_ref) = self.need_migrate_deref_output_trait_object(
826+
source,
827+
obligation.param_env,
828+
&obligation.cause,
829+
) {
830+
if deref_trait_ref.def_id() == target_trait_did {
831+
return;
832+
}
833+
}
783834

784835
for (idx, upcast_trait_ref) in
785836
util::supertraits(self.tcx(), source_trait_ref).enumerate()

library/core/tests/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
#![feature(slice_flatten)]
112112
#![feature(error_generic_member_access)]
113113
#![feature(error_in_core)]
114-
#![cfg_attr(bootstrap, feature(trait_upcasting))]
114+
#![feature(trait_upcasting)]
115115
#![feature(utf8_chunks)]
116116
#![feature(is_ascii_octdigit)]
117117
#![feature(get_many_mut)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# `trait_upcasting`
2+
3+
The tracking issue for this feature is: [#65991]
4+
5+
[#65991]: https://github.com/rust-lang/rust/issues/65991
6+
7+
------------------------
8+
9+
The `trait_upcasting` feature adds support for trait upcasting coercion. This allows a
10+
trait object of type `dyn Bar` to be cast to a trait object of type `dyn Foo`
11+
so long as `Bar: Foo`.
12+
13+
```rust,edition2018
14+
#![feature(trait_upcasting)]
15+
#![allow(incomplete_features)]
16+
17+
trait Foo {}
18+
19+
trait Bar: Foo {}
20+
21+
impl Foo for i32 {}
22+
23+
impl<T: Foo + ?Sized> Bar for T {}
24+
25+
let bar: &dyn Bar = &123;
26+
let foo: &dyn Foo = bar;
27+
```

src/tools/miri/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#![feature(round_ties_even)]
1212
#![feature(let_chains)]
1313
#![feature(lint_reasons)]
14-
#![cfg_attr(bootstrap, feature(trait_upcasting))]
14+
#![feature(trait_upcasting)]
1515
// Configure clippy and other lints
1616
#![allow(
1717
clippy::collapsible_else_if,

src/tools/miri/tests/fail/dyn-upcast-trait-mismatch.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#![feature(trait_upcasting)]
2+
#![allow(incomplete_features)]
3+
14
trait Foo: PartialEq<i32> + std::fmt::Debug + Send + Sync {
25
fn a(&self) -> i32 {
36
10

src/tools/miri/tests/pass/box-custom-alloc.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@revisions: stack tree
22
//@[tree]compile-flags: -Zmiri-tree-borrows
3-
#![feature(allocator_api)]
3+
#![allow(incomplete_features)] // for trait upcasting
4+
#![feature(allocator_api, trait_upcasting)]
45

56
use std::alloc::Layout;
67
use std::alloc::{AllocError, Allocator};

src/tools/miri/tests/pass/dyn-upcast.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#![feature(trait_upcasting)]
2+
#![allow(incomplete_features)]
3+
14
fn main() {
25
basic();
36
diamond();

tests/ui/codegen/issue-99551.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// build-pass
2+
#![feature(trait_upcasting)]
23

34
pub trait A {}
45
pub trait B {}

tests/ui/dyn-star/no-unsize-coerce-dyn-trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(dyn_star)]
1+
#![feature(dyn_star, trait_upcasting)]
22
//~^ WARN the feature `dyn_star` is incomplete and may not be safe to use and/or cause compiler crashes
33

44
trait A: B {}

tests/ui/dyn-star/no-unsize-coerce-dyn-trait.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
warning: the feature `dyn_star` is incomplete and may not be safe to use and/or cause compiler crashes
22
--> $DIR/no-unsize-coerce-dyn-trait.rs:1:12
33
|
4-
LL | #![feature(dyn_star)]
4+
LL | #![feature(dyn_star, trait_upcasting)]
55
| ^^^^^^^^
66
|
77
= note: see issue #102425 <https://github.com/rust-lang/rust/issues/102425> for more information

tests/ui/dyn-star/upcast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// known-bug: #104800
22

3-
#![feature(dyn_star)]
3+
#![feature(dyn_star, trait_upcasting)]
44

55
trait Foo: Bar {
66
fn hello(&self);

tests/ui/dyn-star/upcast.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
warning: the feature `dyn_star` is incomplete and may not be safe to use and/or cause compiler crashes
22
--> $DIR/upcast.rs:3:12
33
|
4-
LL | #![feature(dyn_star)]
4+
LL | #![feature(dyn_star, trait_upcasting)]
55
| ^^^^^^^^
66
|
77
= note: see issue #102425 <https://github.com/rust-lang/rust/issues/102425> for more information
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
trait Foo {}
2+
3+
trait Bar: Foo {}
4+
5+
impl Foo for () {}
6+
7+
impl Bar for () {}
8+
9+
fn main() {
10+
let bar: &dyn Bar = &();
11+
let foo: &dyn Foo = bar;
12+
//~^ ERROR trait upcasting coercion is experimental [E0658]
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0658]: cannot cast `dyn Bar` to `dyn Foo`, trait upcasting coercion is experimental
2+
--> $DIR/feature-gate-trait_upcasting.rs:11:25
3+
|
4+
LL | let foo: &dyn Foo = bar;
5+
| ^^^
6+
|
7+
= note: see issue #65991 <https://github.com/rust-lang/rust/issues/65991> for more information
8+
= help: add `#![feature(trait_upcasting)]` to the crate attributes to enable
9+
= note: required when coercing `&dyn Bar` into `&dyn Foo`
10+
11+
error: aborting due to 1 previous error
12+
13+
For more information about this error, try `rustc --explain E0658`.

tests/ui/traits/next-solver/normalize-unsize-rhs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// compile-flags: -Znext-solver
22
// check-pass
3+
#![feature(trait_upcasting)]
34

45
trait A {}
56
trait B: A {}

tests/ui/traits/next-solver/trait-upcast-lhs-needs-normalization.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// check-pass
22
// compile-flags: -Znext-solver
3+
#![feature(trait_upcasting)]
34

45
pub trait A {}
56
pub trait B: A {}

tests/ui/traits/next-solver/upcast-right-substs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// compile-flags: -Znext-solver
22
// check-pass
3+
#![feature(trait_upcasting)]
34

45
trait Foo: Bar<i32> + Bar<u32> {}
56

tests/ui/traits/trait-upcasting/alias-where-clause-isnt-supertrait.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(trait_upcasting)]
12
#![feature(trait_alias)]
23

34
// Although we *elaborate* `T: Alias` to `i32: B`, we should

tests/ui/traits/trait-upcasting/alias-where-clause-isnt-supertrait.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0308]: mismatched types
2-
--> $DIR/alias-where-clause-isnt-supertrait.rs:26:5
2+
--> $DIR/alias-where-clause-isnt-supertrait.rs:27:5
33
|
44
LL | fn test(x: &dyn C) -> &dyn B {
55
| ------ expected `&dyn B` because of return type

tests/ui/traits/trait-upcasting/basic.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// run-pass
22

3+
#![feature(trait_upcasting)]
4+
35
trait Foo: PartialEq<i32> + std::fmt::Debug + Send + Sync {
46
fn a(&self) -> i32 {
57
10

tests/ui/traits/trait-upcasting/correct-supertrait-substitution.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// run-pass
2+
#![feature(trait_upcasting)]
23

34
trait Foo<T: Default + ToString>: Bar<i32> + Bar<T> {}
45
trait Bar<T: Default + ToString> {

tests/ui/traits/trait-upcasting/deref-lint-regions.stderr

-14
This file was deleted.

0 commit comments

Comments
 (0)