Skip to content

Commit 6bb80eb

Browse files
committed
Port #[rustc_allow_incoherent_impl] to the new attribute system
1 parent aa4d1ab commit 6bb80eb

File tree

7 files changed

+31
-10
lines changed

7 files changed

+31
-10
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

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

203+
/// Represents `#[rustc_allow_incoherent_impl]`.
204+
AllowIncoherentImpl(Span),
205+
203206
/// Represents `#[allow_internal_unstable]`.
204207
AllowInternalUnstable(ThinVec<(Symbol, Span)>, Span),
205208

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ impl AttributeKind {
1515
// tidy-alphabetical-start
1616
Align { .. } => No,
1717
AllowConstFnUnstable(..) => No,
18+
AllowIncoherentImpl(..) => No,
1819
AllowInternalUnstable(..) => Yes,
1920
AsPtr(..) => Yes,
2021
BodyStability { .. } => No,

compiler/rustc_attr_parsing/src/attributes/traits.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ impl<S: Stage> NoArgsAttributeParser<S> for CoinductiveParser {
125125
const CREATE: fn(Span) -> AttributeKind = AttributeKind::Coinductive;
126126
}
127127

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+
128135
pub(crate) struct FundamentalParser;
129136
impl<S: Stage> NoArgsAttributeParser<S> for FundamentalParser {
130137
const PATH: &[Symbol] = &[sym::fundamental];

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ use crate::attributes::stability::{
3636
BodyStabilityParser, ConstStabilityIndirectParser, ConstStabilityParser, StabilityParser,
3737
};
3838
use crate::attributes::traits::{
39-
CoinductiveParser, ConstTraitParser, DenyExplicitImplParser, DoNotImplementViaObjectParser,
40-
FundamentalParser, MarkerParser, ParenSugarParser, SkipDuringMethodDispatchParser,
41-
SpecializationTraitParser, TypeConstParser, UnsafeSpecializationMarkerParser,
39+
AllowIncoherentImplParser, CoinductiveParser, ConstTraitParser, DenyExplicitImplParser,
40+
DoNotImplementViaObjectParser, FundamentalParser, MarkerParser, ParenSugarParser,
41+
SkipDuringMethodDispatchParser, SpecializationTraitParser, TypeConstParser,
42+
UnsafeSpecializationMarkerParser,
4243
};
4344
use crate::attributes::transparency::TransparencyParser;
4445
use crate::attributes::{AttributeParser as _, Combine, Single, WithoutArgs};
@@ -139,6 +140,7 @@ attribute_parsers!(
139140
Single<RustcObjectLifetimeDefaultParser>,
140141
Single<SkipDuringMethodDispatchParser>,
141142
Single<TransparencyParser>,
143+
Single<WithoutArgs<AllowIncoherentImplParser>>,
142144
Single<WithoutArgs<AsPtrParser>>,
143145
Single<WithoutArgs<CoinductiveParser>>,
144146
Single<WithoutArgs<ColdParser>>,

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,

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ fn emit_malformed_attribute(
299299
| sym::const_trait
300300
| sym::rustc_specialization_trait
301301
| sym::rustc_unsafe_specialization_marker
302+
| sym::rustc_allow_incoherent_impl
302303
| sym::marker
303304
| sym::fundamental
304305
| sym::rustc_paren_sugar

compiler/rustc_passes/src/check_attr.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
146146
Attribute::Parsed(AttributeKind::Fundamental) => {
147147
// FIXME: add validation
148148
}
149+
&Attribute::Parsed(AttributeKind::AllowIncoherentImpl(attr_span)) => {
150+
self.check_allow_incoherent_impl(attr_span, span, target)
151+
}
149152
Attribute::Parsed(AttributeKind::Confusables { first_span, .. }) => {
150153
self.check_confusables(*first_span, target);
151154
}
@@ -291,9 +294,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
291294
[sym::collapse_debuginfo, ..] => self.check_collapse_debuginfo(attr, span, target),
292295
[sym::must_not_suspend, ..] => self.check_must_not_suspend(attr, span, target),
293296
[sym::rustc_pass_by_value, ..] => self.check_pass_by_value(attr, span, target),
294-
[sym::rustc_allow_incoherent_impl, ..] => {
295-
self.check_allow_incoherent_impl(attr, span, target)
296-
}
297297
[sym::rustc_has_incoherent_inherent_impls, ..] => {
298298
self.check_has_incoherent_inherent_impls(attr, span, target)
299299
}
@@ -1456,11 +1456,11 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
14561456
}
14571457
}
14581458

1459-
fn check_allow_incoherent_impl(&self, attr: &Attribute, span: Span, target: Target) {
1459+
fn check_allow_incoherent_impl(&self, attr_span: Span, span: Span, target: Target) {
14601460
match target {
14611461
Target::Method(MethodKind::Inherent) => {}
14621462
_ => {
1463-
self.dcx().emit_err(errors::AllowIncoherentImpl { attr_span: attr.span(), span });
1463+
self.dcx().emit_err(errors::AllowIncoherentImpl { attr_span, span });
14641464
}
14651465
}
14661466
}

0 commit comments

Comments
 (0)