Skip to content

Commit a494898

Browse files
Port #[non_exhaustive] to the new attribute parsing infrastructure
1 parent 1b61d43 commit a494898

File tree

17 files changed

+88
-49
lines changed

17 files changed

+88
-49
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@ pub enum AttributeKind {
284284
/// Represents `#[no_mangle]`
285285
NoMangle(Span),
286286

287+
/// Represents `#[non_exhaustive]`
288+
NonExhaustive(Span),
289+
287290
/// Represents `#[optimize(size|speed)]`
288291
Optimize(OptimizeAttr, Span),
289292

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ impl AttributeKind {
2626
Inline(..) => No,
2727
LinkSection { .. } => No,
2828
MacroTransparency(..) => Yes,
29+
NonExhaustive(..) => Yes,
2930
Repr(..) => No,
3031
Stability { .. } => Yes,
3132
Cold(..) => No,

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub(crate) mod lint_helpers;
3636
pub(crate) mod loop_match;
3737
pub(crate) mod must_use;
3838
pub(crate) mod no_implicit_prelude;
39+
pub(crate) mod non_exhaustive;
3940
pub(crate) mod repr;
4041
pub(crate) mod rustc_internal;
4142
pub(crate) mod semantics;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use rustc_attr_data_structures::AttributeKind;
2+
use rustc_span::{Span, Symbol, sym};
3+
4+
use crate::attributes::{NoArgsAttributeParser, OnDuplicate};
5+
use crate::context::Stage;
6+
7+
pub(crate) struct NonExhaustiveParser;
8+
9+
impl<S: Stage> NoArgsAttributeParser<S> for NonExhaustiveParser {
10+
const PATH: &[Symbol] = &[sym::non_exhaustive];
11+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
12+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::NonExhaustive;
13+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use crate::attributes::lint_helpers::{AsPtrParser, PubTransparentParser};
2727
use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
2828
use crate::attributes::must_use::MustUseParser;
2929
use crate::attributes::no_implicit_prelude::NoImplicitPreludeParser;
30+
use crate::attributes::non_exhaustive::NonExhaustiveParser;
3031
use crate::attributes::repr::{AlignParser, ReprParser};
3132
use crate::attributes::rustc_internal::{
3233
RustcLayoutScalarValidRangeEnd, RustcLayoutScalarValidRangeStart,
@@ -144,6 +145,7 @@ attribute_parsers!(
144145
Single<WithoutArgs<MayDangleParser>>,
145146
Single<WithoutArgs<NoImplicitPreludeParser>>,
146147
Single<WithoutArgs<NoMangleParser>>,
148+
Single<WithoutArgs<NonExhaustiveParser>>,
147149
Single<WithoutArgs<PubTransparentParser>>,
148150
Single<WithoutArgs<TrackCallerParser>>,
149151
// tidy-alphabetical-end

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -779,9 +779,11 @@ fn lower_variant<'tcx>(
779779
fields,
780780
parent_did.to_def_id(),
781781
recovered,
782-
adt_kind == AdtKind::Struct && tcx.has_attr(parent_did, sym::non_exhaustive)
783-
|| variant_did
784-
.is_some_and(|variant_did| tcx.has_attr(variant_did, sym::non_exhaustive)),
782+
adt_kind == AdtKind::Struct
783+
&& find_attr!(tcx.get_all_attrs(parent_did), AttributeKind::NonExhaustive(..))
784+
|| variant_did.is_some_and(|variant_did| {
785+
find_attr!(tcx.get_all_attrs(variant_did), AttributeKind::NonExhaustive(..))
786+
}),
785787
)
786788
}
787789

compiler/rustc_middle/src/ty/adt.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::ops::Range;
44
use std::str;
55

66
use rustc_abi::{FIRST_VARIANT, ReprOptions, VariantIdx};
7+
use rustc_attr_data_structures::{AttributeKind, find_attr};
78
use rustc_data_structures::fingerprint::Fingerprint;
89
use rustc_data_structures::fx::FxHashMap;
910
use rustc_data_structures::intern::Interned;
@@ -278,7 +279,9 @@ impl AdtDefData {
278279
debug!("AdtDef::new({:?}, {:?}, {:?}, {:?})", did, kind, variants, repr);
279280
let mut flags = AdtFlags::NO_ADT_FLAGS;
280281

281-
if kind == AdtKind::Enum && tcx.has_attr(did, sym::non_exhaustive) {
282+
if kind == AdtKind::Enum
283+
&& find_attr!(tcx.get_all_attrs(did), AttributeKind::NonExhaustive(..))
284+
{
282285
debug!("found non-exhaustive variant list for {:?}", did);
283286
flags = flags | AdtFlags::IS_VARIANT_LIST_NON_EXHAUSTIVE;
284287
}

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ fn emit_malformed_attribute(
302302
| sym::rustc_allow_const_fn_unstable
303303
| sym::naked
304304
| sym::no_mangle
305+
| sym::non_exhaustive
305306
| sym::must_use
306307
| sym::track_caller
307308
| sym::link_name

compiler/rustc_passes/src/check_attr.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
194194
Attribute::Parsed(AttributeKind::TrackCaller(attr_span)) => {
195195
self.check_track_caller(hir_id, *attr_span, attrs, span, target)
196196
}
197+
Attribute::Parsed(AttributeKind::NonExhaustive(attr_span)) => {
198+
self.check_non_exhaustive(hir_id, *attr_span, span, target, item)
199+
}
197200
Attribute::Parsed(
198201
AttributeKind::RustcLayoutScalarValidRangeStart(_num, attr_span)
199202
| AttributeKind::RustcLayoutScalarValidRangeEnd(_num, attr_span),
@@ -234,7 +237,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
234237
[sym::no_sanitize, ..] => {
235238
self.check_no_sanitize(attr, span, target)
236239
}
237-
[sym::non_exhaustive, ..] => self.check_non_exhaustive(hir_id, attr, span, target, item),
238240
[sym::marker, ..] => self.check_marker(hir_id, attr, span, target),
239241
[sym::thread_local, ..] => self.check_thread_local(attr, span, target),
240242
[sym::doc, ..] => self.check_doc_attrs(
@@ -777,7 +779,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
777779
fn check_non_exhaustive(
778780
&self,
779781
hir_id: HirId,
780-
attr: &Attribute,
782+
attr_span: Span,
781783
span: Span,
782784
target: Target,
783785
item: Option<ItemLike<'_>>,
@@ -792,7 +794,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
792794
&& fields.iter().any(|f| f.default.is_some())
793795
{
794796
self.dcx().emit_err(errors::NonExhaustiveWithDefaultFieldValues {
795-
attr_span: attr.span(),
797+
attr_span,
796798
defn_span: span,
797799
});
798800
}
@@ -803,13 +805,11 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
803805
// erroneously allowed it and some crates used it accidentally, to be compatible
804806
// with crates depending on them, we can't throw an error here.
805807
Target::Field | Target::Arm | Target::MacroDef => {
806-
self.inline_attr_str_error_with_macro_def(hir_id, attr.span(), "non_exhaustive");
808+
self.inline_attr_str_error_with_macro_def(hir_id, attr_span, "non_exhaustive");
807809
}
808810
_ => {
809-
self.dcx().emit_err(errors::NonExhaustiveWrongLocation {
810-
attr_span: attr.span(),
811-
defn_span: span,
812-
});
811+
self.dcx()
812+
.emit_err(errors::NonExhaustiveWrongLocation { attr_span, defn_span: span });
813813
}
814814
}
815815
}

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_ast::{
55
self as ast, CRATE_NODE_ID, Crate, ItemKind, MetaItemInner, MetaItemKind, ModKind, NodeId, Path,
66
};
77
use rustc_ast_pretty::pprust;
8-
use rustc_attr_data_structures::{self as attr, Stability};
8+
use rustc_attr_data_structures::{self as attr, AttributeKind, Stability, find_attr};
99
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1010
use rustc_data_structures::unord::{UnordMap, UnordSet};
1111
use rustc_errors::codes::*;
@@ -1998,9 +1998,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
19981998
// Otherwise, point out if the struct has any private fields.
19991999
if let Some(def_id) = res.opt_def_id()
20002000
&& !def_id.is_local()
2001-
&& let Some(attr) = self.tcx.get_attr(def_id, sym::non_exhaustive)
2001+
&& let Some(attr_span) = find_attr!(self.tcx.get_all_attrs(def_id), AttributeKind::NonExhaustive(span) => *span)
20022002
{
2003-
non_exhaustive = Some(attr.span());
2003+
non_exhaustive = Some(attr_span);
20042004
} else if let Some(span) = ctor_fields_span {
20052005
let label = errors::ConstructorPrivateIfAnyFieldPrivate { span };
20062006
err.subdiagnostic(label);

0 commit comments

Comments
 (0)