Skip to content

Commit 07c2791

Browse files
committed
Replace #[rustc_do_not_implement_via_object] with #[rustc_dyn_incompatible_trait], which makes the marked trait dyn-incompatible.
Removes the attribute from `MetaSized` and `PointeeSized`. `dyn MetaSized` is a perfectly cromulent type, and seems to only have had #[rustc_do_not_implement_via_object] so the builtin object candidate would not overlap with the builtin MetaSized impl that all `dyn` types get. Resolves this by checking `is_sizedness_trait` where the trait solvers previously checked `implement_via_object`. (is this necessary?) `dyn PointeeSized` alone is rejected for other reasons (since `dyn PointeeSized` is considered to have no trait because `PointeeSized` is removed at an earlier stage of the compiler), but `(dyn PointeeSized + Send)` is valid and equivalent to `dyn Send`.
1 parent 6647be9 commit 07c2791

File tree

35 files changed

+383
-155
lines changed

35 files changed

+383
-155
lines changed

compiler/rustc_attr_parsing/src/attributes/traits.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ impl<S: Stage> NoArgsAttributeParser<S> for DenyExplicitImplParser {
9494
const CREATE: fn(Span) -> AttributeKind = AttributeKind::DenyExplicitImpl;
9595
}
9696

97-
pub(crate) struct DoNotImplementViaObjectParser;
98-
impl<S: Stage> NoArgsAttributeParser<S> for DoNotImplementViaObjectParser {
99-
const PATH: &[Symbol] = &[sym::rustc_do_not_implement_via_object];
97+
pub(crate) struct DynIncompatibleTraitParser;
98+
impl<S: Stage> NoArgsAttributeParser<S> for DynIncompatibleTraitParser {
99+
const PATH: &[Symbol] = &[sym::rustc_dyn_incompatible_trait];
100100
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
101101
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Trait)]);
102-
const CREATE: fn(Span) -> AttributeKind = AttributeKind::DoNotImplementViaObject;
102+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::DynIncompatibleTrait;
103103
}
104104

105105
// Specialization

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ use crate::attributes::stability::{
6565
use crate::attributes::test_attrs::{IgnoreParser, ShouldPanicParser};
6666
use crate::attributes::traits::{
6767
AllowIncoherentImplParser, CoinductiveParser, DenyExplicitImplParser,
68-
DoNotImplementViaObjectParser, FundamentalParser, MarkerParser, ParenSugarParser,
69-
PointeeParser, SkipDuringMethodDispatchParser, SpecializationTraitParser, TypeConstParser,
68+
DynIncompatibleTraitParser, FundamentalParser, MarkerParser, ParenSugarParser, PointeeParser,
69+
SkipDuringMethodDispatchParser, SpecializationTraitParser, TypeConstParser,
7070
UnsafeSpecializationMarkerParser,
7171
};
7272
use crate::attributes::transparency::TransparencyParser;
@@ -220,7 +220,7 @@ attribute_parsers!(
220220
Single<WithoutArgs<ConstStabilityIndirectParser>>,
221221
Single<WithoutArgs<CoroutineParser>>,
222222
Single<WithoutArgs<DenyExplicitImplParser>>,
223-
Single<WithoutArgs<DoNotImplementViaObjectParser>>,
223+
Single<WithoutArgs<DynIncompatibleTraitParser>>,
224224
Single<WithoutArgs<ExportStableParser>>,
225225
Single<WithoutArgs<FfiConstParser>>,
226226
Single<WithoutArgs<FfiPureParser>>,

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,13 +1289,13 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
12891289
"`#[rustc_deny_explicit_impl]` enforces that a trait can have no user-provided impls"
12901290
),
12911291
rustc_attr!(
1292-
rustc_do_not_implement_via_object,
1292+
rustc_dyn_incompatible_trait,
12931293
AttributeType::Normal,
12941294
template!(Word),
12951295
ErrorFollowing,
12961296
EncodeCrossCrate::No,
1297-
"`#[rustc_do_not_implement_via_object]` opts out of the automatic trait impl for trait objects \
1298-
(`impl Trait for dyn Trait`)"
1297+
"`#[rustc_dyn_incompatible_trait]` marks a trait as dyn-incompatible, \
1298+
even if it otherwise satisfies the requirements to be dyn-compatible."
12991299
),
13001300
rustc_attr!(
13011301
rustc_has_incoherent_inherent_impls, AttributeType::Normal, template!(Word),

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,15 +520,15 @@ pub enum AttributeKind {
520520
/// Represents [`#[deprecated]`](https://doc.rust-lang.org/stable/reference/attributes/diagnostics.html#the-deprecated-attribute).
521521
Deprecation { deprecation: Deprecation, span: Span },
522522

523-
/// Represents `#[rustc_do_not_implement_via_object]`.
524-
DoNotImplementViaObject(Span),
525-
526523
/// Represents [`#[doc = "..."]`](https://doc.rust-lang.org/stable/rustdoc/write-documentation/the-doc-attribute.html).
527524
DocComment { style: AttrStyle, kind: CommentKind, span: Span, comment: Symbol },
528525

529526
/// Represents `#[rustc_dummy]`.
530527
Dummy,
531528

529+
/// Represents `#[rustc_dyn_incompatible_trait]`.
530+
DynIncompatibleTrait(Span),
531+
532532
/// Represents [`#[export_name]`](https://doc.rust-lang.org/reference/abi.html#the-export_name-attribute).
533533
ExportName {
534534
/// The name to export this item with.

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ impl AttributeKind {
3939
DebuggerVisualizer(..) => No,
4040
DenyExplicitImpl(..) => No,
4141
Deprecation { .. } => Yes,
42-
DoNotImplementViaObject(..) => No,
4342
DocComment { .. } => Yes,
4443
Dummy => No,
44+
DynIncompatibleTrait(..) => Yes, // FIXME: is this needed?
4545
ExportName { .. } => Yes,
4646
ExportStable => No,
4747
FfiConst(..) => No,

compiler/rustc_hir_analysis/src/coherence/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,7 @@ fn check_object_overlap<'tcx>(
211211
// This is a WF error tested by `coherence-impl-trait-for-trait-dyn-compatible.rs`.
212212
} else {
213213
let mut supertrait_def_ids = elaborate::supertrait_def_ids(tcx, component_def_id);
214-
if supertrait_def_ids
215-
.any(|d| d == trait_def_id && tcx.trait_def(d).implement_via_object)
216-
{
214+
if supertrait_def_ids.any(|d| d == trait_def_id) {
217215
let span = tcx.def_span(impl_def_id);
218216
return Err(struct_span_code_err!(
219217
tcx.dcx(),

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,8 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
996996
});
997997

998998
let deny_explicit_impl = find_attr!(attrs, AttributeKind::DenyExplicitImpl(_));
999-
let implement_via_object = !find_attr!(attrs, AttributeKind::DoNotImplementViaObject(_));
999+
let force_dyn_incompatible =
1000+
find_attr!(attrs, AttributeKind::DynIncompatibleTrait(span) => *span);
10001001

10011002
ty::TraitDef {
10021003
def_id: def_id.to_def_id(),
@@ -1011,7 +1012,7 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
10111012
skip_boxed_slice_during_method_dispatch,
10121013
specialization_kind,
10131014
must_implement_one_of,
1014-
implement_via_object,
1015+
force_dyn_incompatible,
10151016
deny_explicit_impl,
10161017
}
10171018
}

compiler/rustc_middle/src/traits/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,9 @@ pub enum DynCompatibilityViolation {
757757
/// `Self: Sized` declared on the trait.
758758
SizedSelf(SmallVec<[Span; 1]>),
759759

760+
/// Trait is marked `#[rustc_dyn_incompatible_trait]`.
761+
ExplicitlyDynIncompatible(SmallVec<[Span; 1]>),
762+
760763
/// Supertrait reference references `Self` an in illegal location
761764
/// (e.g., `trait Foo : Bar<Self>`).
762765
SupertraitSelf(SmallVec<[Span; 1]>),
@@ -781,6 +784,9 @@ impl DynCompatibilityViolation {
781784
pub fn error_msg(&self) -> Cow<'static, str> {
782785
match self {
783786
DynCompatibilityViolation::SizedSelf(_) => "it requires `Self: Sized`".into(),
787+
DynCompatibilityViolation::ExplicitlyDynIncompatible(_) => {
788+
"it opted out of dyn-compatibility".into()
789+
}
784790
DynCompatibilityViolation::SupertraitSelf(spans) => {
785791
if spans.iter().any(|sp| *sp != DUMMY_SP) {
786792
"it uses `Self` as a type parameter".into()
@@ -854,6 +860,7 @@ impl DynCompatibilityViolation {
854860
pub fn solution(&self) -> DynCompatibilityViolationSolution {
855861
match self {
856862
DynCompatibilityViolation::SizedSelf(_)
863+
| DynCompatibilityViolation::ExplicitlyDynIncompatible(_)
857864
| DynCompatibilityViolation::SupertraitSelf(_)
858865
| DynCompatibilityViolation::SupertraitNonLifetimeBinder(..)
859866
| DynCompatibilityViolation::SupertraitConst(_) => {
@@ -887,6 +894,7 @@ impl DynCompatibilityViolation {
887894
match self {
888895
DynCompatibilityViolation::SupertraitSelf(spans)
889896
| DynCompatibilityViolation::SizedSelf(spans)
897+
| DynCompatibilityViolation::ExplicitlyDynIncompatible(spans)
890898
| DynCompatibilityViolation::SupertraitNonLifetimeBinder(spans)
891899
| DynCompatibilityViolation::SupertraitConst(spans) => spans.clone(),
892900
DynCompatibilityViolation::AssocConst(_, span)

compiler/rustc_middle/src/ty/context.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -709,10 +709,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
709709
self.trait_def(def_id).is_fundamental
710710
}
711711

712-
fn trait_may_be_implemented_via_object(self, trait_def_id: DefId) -> bool {
713-
self.trait_def(trait_def_id).implement_via_object
714-
}
715-
716712
fn trait_is_unsafe(self, trait_def_id: Self::DefId) -> bool {
717713
self.trait_def(trait_def_id).safety.is_unsafe()
718714
}

compiler/rustc_middle/src/ty/trait_def.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_hir as hir;
66
use rustc_hir::def::DefKind;
77
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
88
use rustc_macros::{Decodable, Encodable, HashStable};
9+
use rustc_span::Span;
910
use rustc_span::symbol::sym;
1011
use tracing::debug;
1112

@@ -69,10 +70,9 @@ pub struct TraitDef {
6970
/// must be implemented.
7071
pub must_implement_one_of: Option<Box<[Ident]>>,
7172

72-
/// Whether to add a builtin `dyn Trait: Trait` implementation.
73-
/// This is enabled for all traits except ones marked with
74-
/// `#[rustc_do_not_implement_via_object]`.
75-
pub implement_via_object: bool,
73+
/// Whether the trait should be considered dyn-incompatible, even if it otherwise
74+
/// satisfies the requirements to be dyn-compatible.
75+
pub force_dyn_incompatible: Option<Span>,
7676

7777
/// Whether a trait is fully built-in, and any implementation is disallowed.
7878
/// This only applies to built-in traits, and is marked via

0 commit comments

Comments
 (0)