Skip to content

Commit ad635e5

Browse files
committed
Auto merge of #143779 - JonathanBrouwer:automatically_derived_parser, r=oli-obk
Port `#[automatically_derived]` to the new attribute parsing infrastructure Ports `#[automatically_derived]` to the new attribute parsing infrastructure for #131229 (comment) r? `@oli-obk` cc `@jdonszelmann`
2 parents 9c3064e + 68066b9 commit ad635e5

File tree

20 files changed

+92
-50
lines changed

20 files changed

+92
-50
lines changed

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ impl AttributeExt for Attribute {
217217
_ => None,
218218
}
219219
}
220+
221+
fn is_automatically_derived_attr(&self) -> bool {
222+
self.has_name(sym::automatically_derived)
223+
}
220224
}
221225

222226
impl Attribute {
@@ -810,6 +814,7 @@ pub trait AttributeExt: Debug {
810814
.iter()
811815
.any(|kind| self.has_name(*kind))
812816
}
817+
fn is_automatically_derived_attr(&self) -> bool;
813818

814819
/// Returns the documentation and its kind if this is a doc comment or a sugared doc comment.
815820
/// * `///doc` returns `Some(("doc", CommentKind::Line))`.

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ pub enum AttributeKind {
207207
/// Represents `#[rustc_as_ptr]` (used by the `dangling_pointers_from_temporaries` lint).
208208
AsPtr(Span),
209209

210+
/// Represents `#[automatically_derived]`
211+
AutomaticallyDerived(Span),
212+
210213
/// Represents `#[rustc_default_body_unstable]`.
211214
BodyStability {
212215
stability: DefaultBodyStability,

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ impl AttributeKind {
1818
AllowIncoherentImpl(..) => No,
1919
AllowInternalUnstable(..) => Yes,
2020
AsPtr(..) => Yes,
21+
AutomaticallyDerived(..) => Yes,
2122
BodyStability { .. } => No,
2223
CoherenceIsCore => No,
2324
Coinductive(..) => No,

compiler/rustc_attr_parsing/src/attributes/lint_helpers.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,10 @@ impl<S: Stage> NoArgsAttributeParser<S> for PassByValueParser {
2424
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
2525
const CREATE: fn(Span) -> AttributeKind = AttributeKind::PassByValue;
2626
}
27+
28+
pub(crate) struct AutomaticallyDerivedParser;
29+
impl<S: Stage> NoArgsAttributeParser<S> for AutomaticallyDerivedParser {
30+
const PATH: &[Symbol] = &[sym::automatically_derived];
31+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
32+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::AutomaticallyDerived;
33+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ use crate::attributes::link_attrs::{
2727
ExportStableParser, FfiConstParser, FfiPureParser, LinkNameParser, LinkSectionParser,
2828
StdInternalSymbolParser,
2929
};
30-
use crate::attributes::lint_helpers::{AsPtrParser, PassByValueParser, PubTransparentParser};
30+
use crate::attributes::lint_helpers::{
31+
AsPtrParser, AutomaticallyDerivedParser, PassByValueParser, PubTransparentParser,
32+
};
3133
use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
3234
use crate::attributes::must_use::MustUseParser;
3335
use crate::attributes::no_implicit_prelude::NoImplicitPreludeParser;
@@ -153,6 +155,7 @@ attribute_parsers!(
153155
Single<TransparencyParser>,
154156
Single<WithoutArgs<AllowIncoherentImplParser>>,
155157
Single<WithoutArgs<AsPtrParser>>,
158+
Single<WithoutArgs<AutomaticallyDerivedParser>>,
156159
Single<WithoutArgs<CoherenceIsCoreParser>>,
157160
Single<WithoutArgs<CoinductiveParser>>,
158161
Single<WithoutArgs<ColdParser>>,

compiler/rustc_hir/src/hir.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,7 @@ impl AttributeExt for Attribute {
13041304
Attribute::Parsed(AttributeKind::DocComment { span, .. }) => *span,
13051305
Attribute::Parsed(AttributeKind::MayDangle(span)) => *span,
13061306
Attribute::Parsed(AttributeKind::Ignore { span, .. }) => *span,
1307+
Attribute::Parsed(AttributeKind::AutomaticallyDerived(span)) => *span,
13071308
a => panic!("can't get the span of an arbitrary parsed attribute: {a:?}"),
13081309
}
13091310
}
@@ -1334,6 +1335,11 @@ impl AttributeExt for Attribute {
13341335
_ => None,
13351336
}
13361337
}
1338+
1339+
fn is_automatically_derived_attr(&self) -> bool {
1340+
matches!(self, Attribute::Parsed(AttributeKind::AutomaticallyDerived(..)))
1341+
}
1342+
13371343
#[inline]
13381344
fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)> {
13391345
match &self {

compiler/rustc_lint/src/default_could_be_derived.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rustc_attr_data_structures::{AttributeKind, find_attr};
12
use rustc_data_structures::fx::FxHashMap;
23
use rustc_errors::{Applicability, Diag};
34
use rustc_hir as hir;
@@ -62,7 +63,7 @@ impl<'tcx> LateLintPass<'tcx> for DefaultCouldBeDerived {
6263
let hir::ImplItemKind::Fn(_sig, body_id) = impl_item.kind else { return };
6364
let assoc = cx.tcx.associated_item(impl_item.owner_id);
6465
let parent = assoc.container_id(cx.tcx);
65-
if cx.tcx.has_attr(parent, sym::automatically_derived) {
66+
if find_attr!(cx.tcx.get_all_attrs(parent), AttributeKind::AutomaticallyDerived(..)) {
6667
// We don't care about what `#[derive(Default)]` produces in this lint.
6768
return;
6869
}

compiler/rustc_lint/src/levels.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
644644
) {
645645
let sess = self.sess;
646646
for (attr_index, attr) in attrs.iter().enumerate() {
647-
if attr.has_name(sym::automatically_derived) {
647+
if attr.is_automatically_derived_attr() {
648648
self.insert(
649649
LintId::of(SINGLE_USE_LIFETIMES),
650650
LevelAndSource {

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use rustc_abi::{Align, FieldIdx, Integer, IntegerType, ReprFlags, ReprOptions, V
2828
use rustc_ast::expand::StrippedCfgItem;
2929
use rustc_ast::node_id::NodeMap;
3030
pub use rustc_ast_ir::{Movability, Mutability, try_visit};
31-
use rustc_attr_data_structures::AttributeKind;
31+
use rustc_attr_data_structures::{AttributeKind, find_attr};
3232
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
3333
use rustc_data_structures::intern::Interned;
3434
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@@ -2035,7 +2035,7 @@ impl<'tcx> TyCtxt<'tcx> {
20352035

20362036
/// Check if the given `DefId` is `#\[automatically_derived\]`.
20372037
pub fn is_automatically_derived(self, def_id: DefId) -> bool {
2038-
self.has_attr(def_id, sym::automatically_derived)
2038+
find_attr!(self.get_all_attrs(def_id), AttributeKind::AutomaticallyDerived(..))
20392039
}
20402040

20412041
/// Looks up the span of `impl_did` if the impl is local; otherwise returns `Err`

compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use core::ops::ControlFlow;
22

33
use rustc_abi::{FieldIdx, VariantIdx};
44
use rustc_apfloat::Float;
5+
use rustc_attr_data_structures::{AttributeKind, find_attr};
56
use rustc_data_structures::fx::FxHashSet;
67
use rustc_errors::Diag;
78
use rustc_hir as hir;
@@ -15,7 +16,7 @@ use rustc_middle::ty::{
1516
};
1617
use rustc_middle::{mir, span_bug};
1718
use rustc_span::def_id::DefId;
18-
use rustc_span::{DUMMY_SP, Span, sym};
19+
use rustc_span::{DUMMY_SP, Span};
1920
use rustc_trait_selection::traits::ObligationCause;
2021
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
2122
use tracing::{debug, instrument, trace};
@@ -495,7 +496,8 @@ fn type_has_partial_eq_impl<'tcx>(
495496
let mut structural_peq = false;
496497
let mut impl_def_id = None;
497498
for def_id in tcx.non_blanket_impls_for_ty(partial_eq_trait_id, ty) {
498-
automatically_derived = tcx.has_attr(def_id, sym::automatically_derived);
499+
automatically_derived =
500+
find_attr!(tcx.get_all_attrs(def_id), AttributeKind::AutomaticallyDerived(..));
499501
impl_def_id = Some(def_id);
500502
}
501503
for _ in tcx.non_blanket_impls_for_ty(structural_partial_eq_trait_id, ty) {

0 commit comments

Comments
 (0)