Skip to content

Commit 7740e1b

Browse files
committed
Revert "Auto merge of #118133 - Urgau:stabilize_trait_upcasting, r=WaffleLapkin"
This reverts commit 6d2b84b, reversing changes made to 73bc121.
1 parent e8b6db7 commit 7740e1b

File tree

68 files changed

+350
-101
lines changed

Some content is hidden

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

68 files changed

+350
-101
lines changed

compiler/rustc_feature/src/accepted.rs

Lines changed: 0 additions & 3 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 23 additions & 0 deletions
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

Lines changed: 11 additions & 2 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#![feature(iter_intersperse)]
3737
#![feature(iter_order_by)]
3838
#![feature(let_chains)]
39+
#![cfg_attr(not(bootstrap), feature(trait_upcasting))]
3940
#![feature(min_specialization)]
4041
#![feature(never_type)]
4142
#![feature(rustc_attrs)]

compiler/rustc_lint/src/lints.rs

Lines changed: 0 additions & 1 deletion
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_trait_selection/src/traits/select/candidate_assembly.rs

Lines changed: 51 additions & 0 deletions
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()
Lines changed: 27 additions & 0 deletions
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/tests/fail/dyn-upcast-trait-mismatch.rs

Lines changed: 3 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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};

0 commit comments

Comments
 (0)