Skip to content

Commit 8392406

Browse files
authored
Rollup merge of #150942 - port_more_attrs, r=jdonszelmann
Port `#[rustc_has_incoherent_inherent_impls]` to attribute parser Tracking issue: #131229 no tests changed here at all, so maybe we should add some but would like to know what kind of tests would be good to add r? @JonathanBrouwer
2 parents c6cf1c3 + 76fcac2 commit 8392406

File tree

9 files changed

+35
-38
lines changed

9 files changed

+35
-38
lines changed

‎compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,18 @@ impl<S: Stage> SingleAttributeParser<S> for RustcScalableVectorParser {
305305
Some(AttributeKind::RustcScalableVector { element_count: Some(n), span: cx.attr_span })
306306
}
307307
}
308+
309+
pub(crate) struct RustcHasIncoherentInherentImplsParser;
310+
311+
impl<S: Stage> NoArgsAttributeParser<S> for RustcHasIncoherentInherentImplsParser {
312+
const PATH: &[Symbol] = &[sym::rustc_has_incoherent_inherent_impls];
313+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
314+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
315+
Allow(Target::Trait),
316+
Allow(Target::Struct),
317+
Allow(Target::Enum),
318+
Allow(Target::Union),
319+
Allow(Target::ForeignTy),
320+
]);
321+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcHasIncoherentInherentImpls;
322+
}

‎compiler/rustc_attr_parsing/src/context.rs‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ use crate::attributes::proc_macro_attrs::{
6262
use crate::attributes::prototype::CustomMirParser;
6363
use crate::attributes::repr::{AlignParser, AlignStaticParser, ReprParser};
6464
use crate::attributes::rustc_internal::{
65-
RustcLayoutScalarValidRangeEndParser, RustcLayoutScalarValidRangeStartParser,
66-
RustcLegacyConstGenericsParser, RustcLintDiagnosticsParser, RustcLintOptDenyFieldAccessParser,
67-
RustcLintOptTyParser, RustcLintQueryInstabilityParser,
68-
RustcLintUntrackedQueryInformationParser, RustcMainParser, RustcMustImplementOneOfParser,
69-
RustcNeverReturnsNullPointerParser, RustcNoImplicitAutorefsParser,
70-
RustcObjectLifetimeDefaultParser, RustcScalableVectorParser,
65+
RustcHasIncoherentInherentImplsParser, RustcLayoutScalarValidRangeEndParser,
66+
RustcLayoutScalarValidRangeStartParser, RustcLegacyConstGenericsParser,
67+
RustcLintDiagnosticsParser, RustcLintOptDenyFieldAccessParser, RustcLintOptTyParser,
68+
RustcLintQueryInstabilityParser, RustcLintUntrackedQueryInformationParser, RustcMainParser,
69+
RustcMustImplementOneOfParser, RustcNeverReturnsNullPointerParser,
70+
RustcNoImplicitAutorefsParser, RustcObjectLifetimeDefaultParser, RustcScalableVectorParser,
7171
RustcSimdMonomorphizeLaneLimitParser,
7272
};
7373
use crate::attributes::semantics::MayDangleParser;
@@ -264,6 +264,7 @@ attribute_parsers!(
264264
Single<WithoutArgs<ProcMacroParser>>,
265265
Single<WithoutArgs<PubTransparentParser>>,
266266
Single<WithoutArgs<RustcCoherenceIsCoreParser>>,
267+
Single<WithoutArgs<RustcHasIncoherentInherentImplsParser>>,
267268
Single<WithoutArgs<RustcLintDiagnosticsParser>>,
268269
Single<WithoutArgs<RustcLintOptTyParser>>,
269270
Single<WithoutArgs<RustcLintQueryInstabilityParser>>,

‎compiler/rustc_hir/src/attrs/data_structures.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,9 @@ pub enum AttributeKind {
880880
/// Represents `#[rustc_coherence_is_core]`
881881
RustcCoherenceIsCore(Span),
882882

883+
/// Represents `#[rustc_has_incoherent_inherent_impls]`
884+
RustcHasIncoherentInherentImpls,
885+
883886
/// Represents `#[rustc_layout_scalar_valid_range_end]`.
884887
RustcLayoutScalarValidRangeEnd(Box<u128>, Span),
885888

‎compiler/rustc_hir/src/attrs/encode_cross_crate.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ impl AttributeKind {
9595
Repr { .. } => No,
9696
RustcBuiltinMacro { .. } => Yes,
9797
RustcCoherenceIsCore(..) => No,
98+
RustcHasIncoherentInherentImpls => Yes,
9899
RustcLayoutScalarValidRangeEnd(..) => Yes,
99100
RustcLayoutScalarValidRangeStart(..) => Yes,
100101
RustcLegacyConstGenerics { .. } => Yes,

‎compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_hir::find_attr;
1515
use rustc_middle::bug;
1616
use rustc_middle::ty::fast_reject::{SimplifiedType, TreatParams, simplify_type};
1717
use rustc_middle::ty::{self, CrateInherentImpls, Ty, TyCtxt};
18-
use rustc_span::{ErrorGuaranteed, sym};
18+
use rustc_span::ErrorGuaranteed;
1919

2020
use crate::errors;
2121

@@ -79,7 +79,10 @@ impl<'tcx> InherentCollect<'tcx> {
7979
}
8080

8181
if self.tcx.features().rustc_attrs() {
82-
if !self.tcx.has_attr(ty_def_id, sym::rustc_has_incoherent_inherent_impls) {
82+
if !find_attr!(
83+
self.tcx.get_all_attrs(ty_def_id),
84+
AttributeKind::RustcHasIncoherentInherentImpls
85+
) {
8386
let impl_span = self.tcx.def_span(impl_def_id);
8487
return Err(self.tcx.dcx().emit_err(errors::InherentTyOutside { span: impl_span }));
8588
}

‎compiler/rustc_middle/src/ty/trait_def.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ use std::iter;
22

33
use rustc_data_structures::fx::FxIndexMap;
44
use rustc_errors::ErrorGuaranteed;
5-
use rustc_hir as hir;
5+
use rustc_hir::attrs::AttributeKind;
66
use rustc_hir::def::DefKind;
77
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
8+
use rustc_hir::{self as hir, find_attr};
89
use rustc_macros::{Decodable, Encodable, HashStable};
9-
use rustc_span::symbol::sym;
1010
use tracing::debug;
1111

1212
use crate::query::LocalCrate;
@@ -241,7 +241,7 @@ pub(super) fn trait_impls_of_provider(tcx: TyCtxt<'_>, trait_id: DefId) -> Trait
241241
/// Query provider for `incoherent_impls`.
242242
pub(super) fn incoherent_impls_provider(tcx: TyCtxt<'_>, simp: SimplifiedType) -> &[DefId] {
243243
if let Some(def_id) = simp.def()
244-
&& !tcx.has_attr(def_id, sym::rustc_has_incoherent_inherent_impls)
244+
&& !find_attr!(tcx.get_all_attrs(def_id), AttributeKind::RustcHasIncoherentInherentImpls)
245245
{
246246
return &[];
247247
}

‎compiler/rustc_passes/messages.ftl‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,6 @@ passes_function_not_have_default_implementation = function doesn't have a defaul
244244
passes_functions_names_duplicated = functions names are duplicated
245245
.note = all `#[rustc_must_implement_one_of]` arguments must be unique
246246
247-
passes_has_incoherent_inherent_impl =
248-
`rustc_has_incoherent_inherent_impls` attribute should be applied to types or traits
249-
.label = only adts, extern types and traits are supported
250-
251247
passes_ignored_derived_impls =
252248
`{$name}` has {$trait_list_len ->
253249
[one] a derived impl

‎compiler/rustc_passes/src/check_attr.rs‎

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
306306
| AttributeKind::CfgAttrTrace
307307
| AttributeKind::ThreadLocal
308308
| AttributeKind::CfiEncoding { .. }
309+
| AttributeKind::RustcHasIncoherentInherentImpls
309310
) => { /* do nothing */ }
310311
Attribute::Unparsed(attr_item) => {
311312
style = Some(attr_item.style);
@@ -325,9 +326,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
325326
| [sym::rustc_then_this_would_need, ..] => self.check_rustc_dirty_clean(attr),
326327
[sym::collapse_debuginfo, ..] => self.check_collapse_debuginfo(attr, span, target),
327328
[sym::must_not_suspend, ..] => self.check_must_not_suspend(attr, span, target),
328-
[sym::rustc_has_incoherent_inherent_impls, ..] => {
329-
self.check_has_incoherent_inherent_impls(attr, span, target)
330-
}
331329
[sym::autodiff_forward, ..] | [sym::autodiff_reverse, ..] => {
332330
self.check_autodiff(hir_id, attr, span, target)
333331
}
@@ -1164,17 +1162,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
11641162
}
11651163
}
11661164

1167-
fn check_has_incoherent_inherent_impls(&self, attr: &Attribute, span: Span, target: Target) {
1168-
match target {
1169-
Target::Trait | Target::Struct | Target::Enum | Target::Union | Target::ForeignTy => {}
1170-
_ => {
1171-
self.tcx
1172-
.dcx()
1173-
.emit_err(errors::HasIncoherentInherentImpl { attr_span: attr.span(), span });
1174-
}
1175-
}
1176-
}
1177-
11781165
fn check_ffi_pure(&self, attr_span: Span, attrs: &[Attribute]) {
11791166
if find_attr!(attrs, AttributeKind::FfiConst(_)) {
11801167
// `#[ffi_const]` functions cannot be `#[ffi_pure]`

‎compiler/rustc_passes/src/errors.rs‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,6 @@ pub(crate) struct DocAttrNotCrateLevel<'a> {
187187
pub attr_name: &'a str,
188188
}
189189

190-
#[derive(Diagnostic)]
191-
#[diag(passes_has_incoherent_inherent_impl)]
192-
pub(crate) struct HasIncoherentInherentImpl {
193-
#[primary_span]
194-
pub attr_span: Span,
195-
#[label]
196-
pub span: Span,
197-
}
198-
199190
#[derive(Diagnostic)]
200191
#[diag(passes_both_ffi_const_and_pure, code = E0757)]
201192
pub(crate) struct BothFfiConstAndPure {

0 commit comments

Comments
 (0)