Skip to content

Commit c8780ff

Browse files
authored
Rollup merge of #143403 - GrigorenkoPV:attributes/traits, r=jdonszelmann
Port several trait/coherence-related attributes the new attribute system Part of #131229 This ports: - `#[const_trait]` - `#[rustc_deny_explicit_impl]` - `#[rustc_do_not_implement_via_object]` - `#[rustc_coinductive]` - `#[type_const]` - `#[rustc_specialization_trait]` - `#[rustc_unsafe_specialization_marker]` - `#[marker]` - `#[fundamental]` - `#[rustc_paren_sugar]` - `#[rustc_allow_incoherent_impl]` - `#[rustc_coherence_is_core]` This also changes `#[marker]` to error on duplicates instead of warning. cc #142838, but I don't think it matters too much, since it's unstable. r? ``@oli-obk``
2 parents 855e0fe + e584ed0 commit c8780ff

File tree

15 files changed

+299
-90
lines changed

15 files changed

+299
-90
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ pub enum AttributeKind {
198198
/// Represents `#[rustc_allow_const_fn_unstable]`.
199199
AllowConstFnUnstable(ThinVec<Symbol>, Span),
200200

201+
/// Represents `#[rustc_allow_incoherent_impl]`.
202+
AllowIncoherentImpl(Span),
203+
201204
/// Represents `#[allow_internal_unstable]`.
202205
AllowInternalUnstable(ThinVec<(Symbol, Span)>, Span),
203206

@@ -211,6 +214,12 @@ pub enum AttributeKind {
211214
span: Span,
212215
},
213216

217+
/// Represents `#[rustc_coherence_is_core]`.
218+
CoherenceIsCore,
219+
220+
/// Represents `#[rustc_coinductive]`.
221+
Coinductive(Span),
222+
214223
/// Represents `#[cold]`.
215224
Cold(Span),
216225

@@ -234,9 +243,18 @@ pub enum AttributeKind {
234243
/// Represents `#[rustc_const_stable_indirect]`.
235244
ConstStabilityIndirect,
236245

246+
/// Represents `#[const_trait]`.
247+
ConstTrait(Span),
248+
249+
///Represents `#[rustc_deny_explicit_impl]`.
250+
DenyExplicitImpl(Span),
251+
237252
/// Represents [`#[deprecated]`](https://doc.rust-lang.org/stable/reference/attributes/diagnostics.html#the-deprecated-attribute).
238253
Deprecation { deprecation: Deprecation, span: Span },
239254

255+
/// Represents `#[rustc_do_not_implement_via_object]`.
256+
DoNotImplementViaObject(Span),
257+
240258
/// Represents [`#[doc]`](https://doc.rust-lang.org/stable/rustdoc/write-documentation/the-doc-attribute.html).
241259
DocComment { style: AttrStyle, kind: CommentKind, span: Span, comment: Symbol },
242260

@@ -260,6 +278,9 @@ pub enum AttributeKind {
260278
/// Represents `#[ffi_pure]`.
261279
FfiPure(Span),
262280

281+
/// Represents `#[fundamental]`.
282+
Fundamental,
283+
263284
/// Represents `#[ignore]`
264285
Ignore {
265286
span: Span,
@@ -282,6 +303,9 @@ pub enum AttributeKind {
282303
/// Represents `#[rustc_macro_transparency]`.
283304
MacroTransparency(Transparency),
284305

306+
/// Represents `#[marker]`.
307+
Marker(Span),
308+
285309
/// Represents [`#[may_dangle]`](https://std-dev-guide.rust-lang.org/tricky/may-dangle.html).
286310
MayDangle(Span),
287311

@@ -307,6 +331,9 @@ pub enum AttributeKind {
307331
/// Represents `#[optimize(size|speed)]`
308332
Optimize(OptimizeAttr, Span),
309333

334+
/// Represents `#[rustc_paren_sugar]`.
335+
ParenSugar(Span),
336+
310337
/// Represents `#[rustc_pass_by_value]` (used by the `rustc_pass_by_value` lint).
311338
PassByValue(Span),
312339

@@ -331,6 +358,9 @@ pub enum AttributeKind {
331358
/// Represents `#[rustc_skip_during_method_dispatch]`.
332359
SkipDuringMethodDispatch { array: bool, boxed_slice: bool, span: Span },
333360

361+
/// Represents `#[rustc_specialization_trait]`.
362+
SpecializationTrait(Span),
363+
334364
/// Represents `#[stable]`, `#[unstable]` and `#[rustc_allowed_through_unstable_modules]`.
335365
Stability {
336366
stability: Stability,
@@ -347,6 +377,12 @@ pub enum AttributeKind {
347377
/// Represents `#[track_caller]`
348378
TrackCaller(Span),
349379

380+
/// Represents `#[type_const]`.
381+
TypeConst(Span),
382+
383+
/// Represents `#[rustc_unsafe_specialization_marker]`.
384+
UnsafeSpecializationMarker(Span),
385+
350386
/// Represents `#[used]`
351387
Used { used_by: UsedBy, span: Span },
352388
// tidy-alphabetical-end

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,43 @@ impl AttributeKind {
1515
// tidy-alphabetical-start
1616
Align { .. } => No,
1717
AllowConstFnUnstable(..) => No,
18+
AllowIncoherentImpl(..) => No,
1819
AllowInternalUnstable(..) => Yes,
1920
AsPtr(..) => Yes,
2021
BodyStability { .. } => No,
22+
CoherenceIsCore => No,
23+
Coinductive(..) => No,
2124
Cold(..) => No,
2225
Confusables { .. } => Yes,
2326
ConstContinue(..) => No,
2427
ConstStability { .. } => Yes,
2528
ConstStabilityIndirect => No,
29+
ConstTrait(..) => No,
30+
DenyExplicitImpl(..) => No,
2631
Deprecation { .. } => Yes,
32+
DoNotImplementViaObject(..) => No,
2733
DocComment { .. } => Yes,
2834
Dummy => No,
2935
ExportName { .. } => Yes,
3036
ExportStable => No,
3137
FfiConst(..) => No,
3238
FfiPure(..) => No,
39+
Fundamental { .. } => Yes,
3340
Ignore { .. } => No,
3441
Inline(..) => No,
3542
LinkName { .. } => Yes,
3643
LinkSection { .. } => No,
3744
LoopMatch(..) => No,
3845
MacroTransparency(..) => Yes,
46+
Marker(..) => No,
3947
MayDangle(..) => No,
4048
MustUse { .. } => Yes,
4149
Naked(..) => No,
4250
NoImplicitPrelude(..) => No,
4351
NoMangle(..) => No,
4452
NonExhaustive(..) => Yes,
4553
Optimize(..) => No,
54+
ParenSugar(..) => No,
4655
PassByValue(..) => Yes,
4756
Path(..) => No,
4857
PubTransparent(..) => Yes,
@@ -51,10 +60,13 @@ impl AttributeKind {
5160
RustcLayoutScalarValidRangeStart(..) => Yes,
5261
RustcObjectLifetimeDefault => No,
5362
SkipDuringMethodDispatch { .. } => No,
63+
SpecializationTrait(..) => No,
5464
Stability { .. } => Yes,
5565
StdInternalSymbol(..) => No,
5666
TargetFeature(..) => No,
5767
TrackCaller(..) => Yes,
68+
TypeConst(..) => Yes,
69+
UnsafeSpecializationMarker(..) => No,
5870
Used { .. } => No,
5971
// tidy-alphabetical-end
6072
}

compiler/rustc_attr_parsing/src/attributes/traits.rs

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ use core::mem;
22

33
use rustc_attr_data_structures::AttributeKind;
44
use rustc_feature::{AttributeTemplate, template};
5-
use rustc_span::{Symbol, sym};
5+
use rustc_span::{Span, Symbol, sym};
66

7-
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser};
7+
use crate::attributes::{
8+
AttributeOrder, NoArgsAttributeParser, OnDuplicate, SingleAttributeParser,
9+
};
810
use crate::context::{AcceptContext, Stage};
911
use crate::parser::ArgParser;
1012

1113
pub(crate) struct SkipDuringMethodDispatchParser;
12-
1314
impl<S: Stage> SingleAttributeParser<S> for SkipDuringMethodDispatchParser {
1415
const PATH: &[Symbol] = &[sym::rustc_skip_during_method_dispatch];
1516
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
@@ -52,3 +53,95 @@ impl<S: Stage> SingleAttributeParser<S> for SkipDuringMethodDispatchParser {
5253
Some(AttributeKind::SkipDuringMethodDispatch { array, boxed_slice, span: cx.attr_span })
5354
}
5455
}
56+
57+
pub(crate) struct ParenSugarParser;
58+
impl<S: Stage> NoArgsAttributeParser<S> for ParenSugarParser {
59+
const PATH: &[Symbol] = &[sym::rustc_paren_sugar];
60+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
61+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::ParenSugar;
62+
}
63+
64+
pub(crate) struct TypeConstParser;
65+
impl<S: Stage> NoArgsAttributeParser<S> for TypeConstParser {
66+
const PATH: &[Symbol] = &[sym::type_const];
67+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
68+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::TypeConst;
69+
}
70+
71+
// Markers
72+
73+
pub(crate) struct MarkerParser;
74+
impl<S: Stage> NoArgsAttributeParser<S> for MarkerParser {
75+
const PATH: &[Symbol] = &[sym::marker];
76+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
77+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::Marker;
78+
}
79+
80+
pub(crate) struct DenyExplicitImplParser;
81+
impl<S: Stage> NoArgsAttributeParser<S> for DenyExplicitImplParser {
82+
const PATH: &[Symbol] = &[sym::rustc_deny_explicit_impl];
83+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
84+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::DenyExplicitImpl;
85+
}
86+
87+
pub(crate) struct DoNotImplementViaObjectParser;
88+
impl<S: Stage> NoArgsAttributeParser<S> for DoNotImplementViaObjectParser {
89+
const PATH: &[Symbol] = &[sym::rustc_do_not_implement_via_object];
90+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
91+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::DoNotImplementViaObject;
92+
}
93+
94+
// Const traits
95+
96+
pub(crate) struct ConstTraitParser;
97+
impl<S: Stage> NoArgsAttributeParser<S> for ConstTraitParser {
98+
const PATH: &[Symbol] = &[sym::const_trait];
99+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
100+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::ConstTrait;
101+
}
102+
103+
// Specialization
104+
105+
pub(crate) struct SpecializationTraitParser;
106+
impl<S: Stage> NoArgsAttributeParser<S> for SpecializationTraitParser {
107+
const PATH: &[Symbol] = &[sym::rustc_specialization_trait];
108+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
109+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::SpecializationTrait;
110+
}
111+
112+
pub(crate) struct UnsafeSpecializationMarkerParser;
113+
impl<S: Stage> NoArgsAttributeParser<S> for UnsafeSpecializationMarkerParser {
114+
const PATH: &[Symbol] = &[sym::rustc_unsafe_specialization_marker];
115+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
116+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::UnsafeSpecializationMarker;
117+
}
118+
119+
// Coherence
120+
121+
pub(crate) struct CoinductiveParser;
122+
impl<S: Stage> NoArgsAttributeParser<S> for CoinductiveParser {
123+
const PATH: &[Symbol] = &[sym::rustc_coinductive];
124+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
125+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::Coinductive;
126+
}
127+
128+
pub(crate) struct AllowIncoherentImplParser;
129+
impl<S: Stage> NoArgsAttributeParser<S> for AllowIncoherentImplParser {
130+
const PATH: &[Symbol] = &[sym::rustc_allow_incoherent_impl];
131+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
132+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::AllowIncoherentImpl;
133+
}
134+
135+
pub(crate) struct CoherenceIsCoreParser;
136+
impl<S: Stage> NoArgsAttributeParser<S> for CoherenceIsCoreParser {
137+
const PATH: &[Symbol] = &[sym::rustc_coherence_is_core];
138+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
139+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::CoherenceIsCore;
140+
}
141+
142+
pub(crate) struct FundamentalParser;
143+
impl<S: Stage> NoArgsAttributeParser<S> for FundamentalParser {
144+
const PATH: &[Symbol] = &[sym::fundamental];
145+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
146+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::Fundamental;
147+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ use crate::attributes::stability::{
4343
BodyStabilityParser, ConstStabilityIndirectParser, ConstStabilityParser, StabilityParser,
4444
};
4545
use crate::attributes::test_attrs::IgnoreParser;
46-
use crate::attributes::traits::SkipDuringMethodDispatchParser;
46+
use crate::attributes::traits::{
47+
AllowIncoherentImplParser, CoherenceIsCoreParser, CoinductiveParser, ConstTraitParser,
48+
DenyExplicitImplParser, DoNotImplementViaObjectParser, FundamentalParser, MarkerParser,
49+
ParenSugarParser, SkipDuringMethodDispatchParser, SpecializationTraitParser, TypeConstParser,
50+
UnsafeSpecializationMarkerParser,
51+
};
4752
use crate::attributes::transparency::TransparencyParser;
4853
use crate::attributes::{AttributeParser as _, Combine, Single, WithoutArgs};
4954
use crate::parser::{ArgParser, MetaItemParser, PathParser};
@@ -146,22 +151,34 @@ attribute_parsers!(
146151
Single<RustcObjectLifetimeDefaultParser>,
147152
Single<SkipDuringMethodDispatchParser>,
148153
Single<TransparencyParser>,
154+
Single<WithoutArgs<AllowIncoherentImplParser>>,
149155
Single<WithoutArgs<AsPtrParser>>,
156+
Single<WithoutArgs<CoherenceIsCoreParser>>,
157+
Single<WithoutArgs<CoinductiveParser>>,
150158
Single<WithoutArgs<ColdParser>>,
151159
Single<WithoutArgs<ConstContinueParser>>,
152160
Single<WithoutArgs<ConstStabilityIndirectParser>>,
161+
Single<WithoutArgs<ConstTraitParser>>,
162+
Single<WithoutArgs<DenyExplicitImplParser>>,
163+
Single<WithoutArgs<DoNotImplementViaObjectParser>>,
153164
Single<WithoutArgs<ExportStableParser>>,
154165
Single<WithoutArgs<FfiConstParser>>,
155166
Single<WithoutArgs<FfiPureParser>>,
167+
Single<WithoutArgs<FundamentalParser>>,
156168
Single<WithoutArgs<LoopMatchParser>>,
169+
Single<WithoutArgs<MarkerParser>>,
157170
Single<WithoutArgs<MayDangleParser>>,
158171
Single<WithoutArgs<NoImplicitPreludeParser>>,
159172
Single<WithoutArgs<NoMangleParser>>,
160173
Single<WithoutArgs<NonExhaustiveParser>>,
174+
Single<WithoutArgs<ParenSugarParser>>,
161175
Single<WithoutArgs<PassByValueParser>>,
162176
Single<WithoutArgs<PubTransparentParser>>,
177+
Single<WithoutArgs<SpecializationTraitParser>>,
163178
Single<WithoutArgs<StdInternalSymbolParser>>,
164179
Single<WithoutArgs<TrackCallerParser>>,
180+
Single<WithoutArgs<TypeConstParser>>,
181+
Single<WithoutArgs<UnsafeSpecializationMarkerParser>>,
165182
// tidy-alphabetical-end
166183
];
167184
);

compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//! `tcx.inherent_impls(def_id)`). That value, however,
88
//! is computed by selecting an idea from this table.
99
10+
use rustc_attr_data_structures::{AttributeKind, find_attr};
1011
use rustc_hir as hir;
1112
use rustc_hir::def::DefKind;
1213
use rustc_hir::def_id::{DefId, LocalDefId};
@@ -85,7 +86,10 @@ impl<'tcx> InherentCollect<'tcx> {
8586
}
8687

8788
for &impl_item in items {
88-
if !self.tcx.has_attr(impl_item, sym::rustc_allow_incoherent_impl) {
89+
if !find_attr!(
90+
self.tcx.get_all_attrs(impl_item),
91+
AttributeKind::AllowIncoherentImpl(_)
92+
) {
8993
let impl_span = self.tcx.def_span(impl_def_id);
9094
return Err(self.tcx.dcx().emit_err(errors::InherentTyOutsideRelevant {
9195
span: impl_span,
@@ -116,7 +120,10 @@ impl<'tcx> InherentCollect<'tcx> {
116120
if !self.tcx.hir_rustc_coherence_is_core() {
117121
if self.tcx.features().rustc_attrs() {
118122
for &impl_item in items {
119-
if !self.tcx.has_attr(impl_item, sym::rustc_allow_incoherent_impl) {
123+
if !find_attr!(
124+
self.tcx.get_all_attrs(impl_item),
125+
AttributeKind::AllowIncoherentImpl(_)
126+
) {
120127
let span = self.tcx.def_span(impl_def_id);
121128
return Err(self.tcx.dcx().emit_err(errors::InherentTyOutsidePrimitive {
122129
span,

0 commit comments

Comments
 (0)