Skip to content

Commit d22b4c7

Browse files
committed
Port #[rustc_allow_incoherent_impl] to the new attribute system
1 parent 7ecc5fb commit d22b4c7

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
@@ -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

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
@@ -40,9 +40,10 @@ use crate::attributes::stability::{
4040
};
4141
use crate::attributes::test_attrs::IgnoreParser;
4242
use crate::attributes::traits::{
43-
CoinductiveParser, ConstTraitParser, DenyExplicitImplParser, DoNotImplementViaObjectParser,
44-
FundamentalParser, MarkerParser, ParenSugarParser, SkipDuringMethodDispatchParser,
45-
SpecializationTraitParser, TypeConstParser, UnsafeSpecializationMarkerParser,
43+
AllowIncoherentImplParser, CoinductiveParser, ConstTraitParser, DenyExplicitImplParser,
44+
DoNotImplementViaObjectParser, FundamentalParser, MarkerParser, ParenSugarParser,
45+
SkipDuringMethodDispatchParser, SpecializationTraitParser, TypeConstParser,
46+
UnsafeSpecializationMarkerParser,
4647
};
4748
use crate::attributes::transparency::TransparencyParser;
4849
use crate::attributes::{AttributeParser as _, Combine, Single, WithoutArgs};
@@ -145,6 +146,7 @@ attribute_parsers!(
145146
Single<RustcObjectLifetimeDefaultParser>,
146147
Single<SkipDuringMethodDispatchParser>,
147148
Single<TransparencyParser>,
149+
Single<WithoutArgs<AllowIncoherentImplParser>>,
148150
Single<WithoutArgs<AsPtrParser>>,
149151
Single<WithoutArgs<CoinductiveParser>>,
150152
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
@@ -285,6 +285,7 @@ pub fn check_builtin_meta_item(
285285
| sym::const_trait
286286
| sym::rustc_specialization_trait
287287
| sym::rustc_unsafe_specialization_marker
288+
| sym::rustc_allow_incoherent_impl
288289
| sym::marker
289290
| sym::fundamental
290291
| 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
}
@@ -309,9 +312,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
309312
[sym::rustc_must_implement_one_of, ..] => self.check_must_be_applied_to_trait(attr.span(), span, target),
310313
[sym::collapse_debuginfo, ..] => self.check_collapse_debuginfo(attr, span, target),
311314
[sym::must_not_suspend, ..] => self.check_must_not_suspend(attr, span, target),
312-
[sym::rustc_allow_incoherent_impl, ..] => {
313-
self.check_allow_incoherent_impl(attr, span, target)
314-
}
315315
[sym::rustc_has_incoherent_inherent_impls, ..] => {
316316
self.check_has_incoherent_inherent_impls(attr, span, target)
317317
}
@@ -1491,11 +1491,11 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
14911491
}
14921492
}
14931493

1494-
fn check_allow_incoherent_impl(&self, attr: &Attribute, span: Span, target: Target) {
1494+
fn check_allow_incoherent_impl(&self, attr_span: Span, span: Span, target: Target) {
14951495
match target {
14961496
Target::Method(MethodKind::Inherent) => {}
14971497
_ => {
1498-
self.dcx().emit_err(errors::AllowIncoherentImpl { attr_span: attr.span(), span });
1498+
self.dcx().emit_err(errors::AllowIncoherentImpl { attr_span, span });
14991499
}
15001500
}
15011501
}

0 commit comments

Comments
 (0)