Skip to content

Commit 85fed0e

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 c90bcb9 commit 85fed0e

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
@@ -93,12 +93,12 @@ impl<S: Stage> NoArgsAttributeParser<S> for DenyExplicitImplParser {
9393
const CREATE: fn(Span) -> AttributeKind = AttributeKind::DenyExplicitImpl;
9494
}
9595

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

104104
// FIXME(const_trait_impl): remove this

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, ConstTraitParser, 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;
@@ -221,7 +221,7 @@ attribute_parsers!(
221221
Single<WithoutArgs<ConstTraitParser>>,
222222
Single<WithoutArgs<CoroutineParser>>,
223223
Single<WithoutArgs<DenyExplicitImplParser>>,
224-
Single<WithoutArgs<DoNotImplementViaObjectParser>>,
224+
Single<WithoutArgs<DynIncompatibleTraitParser>>,
225225
Single<WithoutArgs<ExportStableParser>>,
226226
Single<WithoutArgs<FfiConstParser>>,
227227
Single<WithoutArgs<FfiPureParser>>,

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,13 +1297,13 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
12971297
"`#[rustc_deny_explicit_impl]` enforces that a trait can have no user-provided impls"
12981298
),
12991299
rustc_attr!(
1300-
rustc_do_not_implement_via_object,
1300+
rustc_dyn_incompatible_trait,
13011301
AttributeType::Normal,
13021302
template!(Word),
13031303
ErrorFollowing,
13041304
EncodeCrossCrate::No,
1305-
"`#[rustc_do_not_implement_via_object]` opts out of the automatic trait impl for trait objects \
1306-
(`impl Trait for dyn Trait`)"
1305+
"`#[rustc_dyn_incompatible_trait]` marks a trait as dyn-incompatible, \
1306+
even if it otherwise satisfies the requirements to be dyn-comaptible."
13071307
),
13081308
rustc_attr!(
13091309
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
@@ -513,15 +513,15 @@ pub enum AttributeKind {
513513
/// Represents [`#[deprecated]`](https://doc.rust-lang.org/stable/reference/attributes/diagnostics.html#the-deprecated-attribute).
514514
Deprecation { deprecation: Deprecation, span: Span },
515515

516-
/// Represents `#[rustc_do_not_implement_via_object]`.
517-
DoNotImplementViaObject(Span),
518-
519516
/// Represents [`#[doc = "..."]`](https://doc.rust-lang.org/stable/rustdoc/write-documentation/the-doc-attribute.html).
520517
DocComment { style: AttrStyle, kind: CommentKind, span: Span, comment: Symbol },
521518

522519
/// Represents `#[rustc_dummy]`.
523520
Dummy,
524521

522+
/// Represents `#[rustc_dyn_incompatible_trait]`.
523+
DynIncompatibleTrait(Span),
524+
525525
/// Represents [`#[export_name]`](https://doc.rust-lang.org/reference/abi.html#the-export_name-attribute).
526526
ExportName {
527527
/// 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
@@ -40,9 +40,9 @@ impl AttributeKind {
4040
DebuggerVisualizer(..) => No,
4141
DenyExplicitImpl(..) => No,
4242
Deprecation { .. } => Yes,
43-
DoNotImplementViaObject(..) => No,
4443
DocComment { .. } => Yes,
4544
Dummy => No,
45+
DynIncompatibleTrait(..) => Yes, // FIXME: is this needed?
4646
ExportName { .. } => Yes,
4747
ExportStable => No,
4848
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
@@ -1004,7 +1004,8 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
10041004
});
10051005

10061006
let deny_explicit_impl = find_attr!(attrs, AttributeKind::DenyExplicitImpl(_));
1007-
let implement_via_object = !find_attr!(attrs, AttributeKind::DoNotImplementViaObject(_));
1007+
let force_dyn_incompatible =
1008+
find_attr!(attrs, AttributeKind::DynIncompatibleTrait(span) => *span);
10081009

10091010
ty::TraitDef {
10101011
def_id: def_id.to_def_id(),
@@ -1019,7 +1020,7 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
10191020
skip_boxed_slice_during_method_dispatch,
10201021
specialization_kind,
10211022
must_implement_one_of,
1022-
implement_via_object,
1023+
force_dyn_incompatible,
10231024
deny_explicit_impl,
10241025
}
10251026
}

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-compatbility".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
@@ -706,10 +706,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
706706
self.trait_def(def_id).is_fundamental
707707
}
708708

709-
fn trait_may_be_implemented_via_object(self, trait_def_id: DefId) -> bool {
710-
self.trait_def(trait_def_id).implement_via_object
711-
}
712-
713709
fn trait_is_unsafe(self, trait_def_id: Self::DefId) -> bool {
714710
self.trait_def(trait_def_id).safety.is_unsafe()
715711
}

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)