Skip to content

Commit 4811ba2

Browse files
committed
refactor: more general terminology and use existing attr parse machinery
1 parent 0a02f2d commit 4811ba2

File tree

10 files changed

+113
-107
lines changed

10 files changed

+113
-107
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ struct LoweringContext<'a, 'hir, R> {
155155

156156
impl<'a, 'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'a, 'hir, R> {
157157
fn new(tcx: TyCtxt<'hir>, resolver: &'a mut R) -> Self {
158-
let registered_tools = tcx.registered_tools(()).iter().map(|x| x.name).collect();
158+
let registered_tools = tcx.registered_tools(());
159159
let attr_res_map = resolver.all_attr_resolutions();
160160
Self {
161161
tcx,

compiler/rustc_attr_parsing/src/attributes/repr.rs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_abi::{Align, Size};
22
use rustc_ast::{IntTy, LitIntType, LitKind, UintTy};
3-
use rustc_hir::attrs::{AttrConstResolved, AttrIntValue, IntType, ReprAttr};
3+
use rustc_hir::attrs::{AttrIntValue, AttrResolutionKind, AttrResolved, IntType, ReprAttr};
44
use rustc_hir::def::{DefKind, Res};
55
use rustc_session::parse::feature_err;
66

@@ -105,7 +105,10 @@ fn int_type_of_word(s: Symbol) -> Option<IntType> {
105105
}
106106
}
107107

108-
fn parse_repr<S: Stage>(cx: &AcceptContext<'_, '_, S>, param: &MetaItemParser) -> Option<ReprAttr> {
108+
fn parse_repr<S: Stage>(
109+
cx: &mut AcceptContext<'_, '_, S>,
110+
param: &MetaItemParser,
111+
) -> Option<ReprAttr> {
109112
use ReprAttr::*;
110113

111114
// FIXME(jdonszelmann): invert the parsing here to match on the word first and then the
@@ -200,7 +203,7 @@ enum AlignmentParseError {
200203
}
201204

202205
fn parse_repr_align<S: Stage>(
203-
cx: &AcceptContext<'_, '_, S>,
206+
cx: &mut AcceptContext<'_, '_, S>,
204207
list: &MetaItemListParser,
205208
param_span: Span,
206209
align_kind: AlignKind,
@@ -283,7 +286,7 @@ fn parse_alignment<S: Stage>(
283286
}
284287

285288
fn parse_alignment_or_const_path<S: Stage>(
286-
cx: &AcceptContext<'_, '_, S>,
289+
cx: &mut AcceptContext<'_, '_, S>,
287290
arg: &MetaItemOrLitParser,
288291
attr_name: &'static str,
289292
) -> Result<AttrIntValue, AlignmentParseError> {
@@ -301,10 +304,15 @@ fn parse_alignment_or_const_path<S: Stage>(
301304
return Err(AlignmentParseError::Message("not an unsuffixed integer".to_string()));
302305
}
303306

304-
if let Some(features) = cx.features_option()
305-
&& !features.const_attr_paths()
306-
&& !meta.span().allows_unstable(sym::const_attr_paths)
307-
{
307+
let path_span = meta.path().span();
308+
let feature_enabled = cx.features_option().is_some_and(|features| features.const_attr_paths())
309+
|| path_span.allows_unstable(sym::const_attr_paths);
310+
311+
if !feature_enabled {
312+
if matches!(cx.stage.should_emit(), ShouldEmit::Nothing) {
313+
return Ok(AttrIntValue::Lit(1));
314+
}
315+
308316
feature_err(
309317
cx.sess(),
310318
sym::const_attr_paths,
@@ -315,7 +323,9 @@ fn parse_alignment_or_const_path<S: Stage>(
315323
return Err(AlignmentParseError::AlreadyErrored);
316324
}
317325

318-
let Some(resolution) = cx.attr_const_resolution(meta.path().span()) else {
326+
cx.record_attr_resolution_request(AttrResolutionKind::Const, meta.path().0.clone());
327+
328+
let Some(resolution) = cx.attr_resolution(AttrResolutionKind::Const, path_span) else {
319329
// `parse_limited(sym::repr)` runs before lowering for callers that only care whether
320330
// `repr(packed(...))` exists at all.
321331
if matches!(cx.stage.should_emit(), ShouldEmit::Nothing) {
@@ -325,22 +335,18 @@ fn parse_alignment_or_const_path<S: Stage>(
325335
};
326336

327337
match resolution {
328-
AttrConstResolved::Resolved(Res::Def(DefKind::Const { .. }, def_id)) => {
329-
Ok(AttrIntValue::Const { def_id, span: meta.path().span() })
338+
AttrResolved::Resolved(Res::Def(DefKind::Const { .. }, def_id)) => {
339+
Ok(AttrIntValue::Const { def_id, span: path_span })
330340
}
331-
AttrConstResolved::Resolved(Res::Def(DefKind::ConstParam, _)) => {
332-
cx.emit_err(AttrConstGenericNotSupported { span: meta.path().span(), attr_name });
341+
AttrResolved::Resolved(Res::Def(DefKind::ConstParam, _)) => {
342+
cx.emit_err(AttrConstGenericNotSupported { span: path_span, attr_name });
333343
Err(AlignmentParseError::AlreadyErrored)
334344
}
335-
AttrConstResolved::Resolved(res) => {
336-
cx.emit_err(AttrConstPathNotConst {
337-
span: meta.path().span(),
338-
attr_name,
339-
thing: res.descr(),
340-
});
345+
AttrResolved::Resolved(res) => {
346+
cx.emit_err(AttrConstPathNotConst { span: path_span, attr_name, thing: res.descr() });
341347
Err(AlignmentParseError::AlreadyErrored)
342348
}
343-
AttrConstResolved::Error => Err(AlignmentParseError::AlreadyErrored),
349+
AttrResolved::Error => Err(AlignmentParseError::AlreadyErrored),
344350
}
345351
}
346352

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use private::Sealed;
99
use rustc_ast::{AttrStyle, MetaItemLit, NodeId};
1010
use rustc_errors::{Diag, Diagnostic, Level};
1111
use rustc_feature::{AttrSuggestionStyle, AttributeTemplate};
12-
use rustc_hir::attrs::AttributeKind;
12+
use rustc_hir::attrs::{AttrResolutionKind, AttributeKind};
1313
use rustc_hir::lints::AttributeLintKind;
1414
use rustc_hir::{AttrPath, HirId};
1515
use rustc_parse::parser::Recovery;
@@ -416,7 +416,6 @@ pub struct Late;
416416
/// Gives [`AttributeParser`]s enough information to create errors, for example.
417417
pub struct AcceptContext<'f, 'sess, S: Stage> {
418418
pub(crate) shared: SharedContext<'f, 'sess, S>,
419-
pub(crate) attr_id: Option<AttrId>,
420419

421420
/// The outer span of the attribute currently being parsed
422421
///
@@ -502,13 +501,25 @@ impl<'f, 'sess: 'f, S: Stage> SharedContext<'f, 'sess, S> {
502501
impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
503502
pub(crate) fn adcx(&mut self) -> AttributeDiagnosticContext<'_, 'f, 'sess, S> {
504503
AttributeDiagnosticContext { ctx: self, custom_suggestions: Vec::new() }
504+
}
505505

506-
pub(crate) fn attr_const_resolution(
506+
pub(crate) fn attr_resolution(
507507
&self,
508+
kind: AttrResolutionKind,
508509
path_span: Span,
509-
) -> Option<rustc_hir::attrs::AttrConstResolved<NodeId>> {
510-
self.attr_id.and_then(|attr_id| self.shared.cx.attr_const_resolution(attr_id, path_span))
510+
) -> Option<rustc_hir::attrs::AttrResolved<NodeId>> {
511+
self.shared.cx.attr_resolution(self.attr_id, kind, path_span)
511512
}
513+
514+
pub(crate) fn record_attr_resolution_request(
515+
&mut self,
516+
kind: AttrResolutionKind,
517+
path: rustc_ast::Path,
518+
) {
519+
self.shared.cx.record_attr_resolution_request(
520+
self.attr_id,
521+
crate::interface::AttrResolutionRequest { kind, path },
522+
);
512523
}
513524
}
514525

compiler/rustc_attr_parsing/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,5 @@ pub use attributes::cfg::{
112112
pub use attributes::cfg_select::*;
113113
pub use attributes::util::{is_builtin_attr, parse_version};
114114
pub use context::{Early, Late, OmitDoc, ShouldEmit};
115-
pub use interface::AttributeParser;
115+
pub use interface::{AttrResolutionRequest, AttributeParser};
116116
pub use session_diagnostics::ParsedDescription;

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,16 +184,35 @@ pub enum AttrIntValue {
184184
Const { def_id: DefId, span: Span },
185185
}
186186

187+
/// The resolution strategy a parsed builtin attribute argument expects.
188+
#[derive(
189+
Copy,
190+
Clone,
191+
Debug,
192+
PartialEq,
193+
Eq,
194+
HashStable_Generic,
195+
Encodable,
196+
Decodable,
197+
PrintAttribute
198+
)]
199+
pub enum AttrResolutionKind {
200+
Const,
201+
}
202+
203+
/// A resolved attribute argument path, or an error placeholder if resolution failed.
187204
#[derive(Debug, Copy, Clone, HashStable_Generic, Encodable, Decodable, PrintAttribute)]
188-
pub enum AttrConstResolved<Id = ast::NodeId> {
205+
pub enum AttrResolved<Id = ast::NodeId> {
189206
Resolved(Res<Id>),
190207
Error,
191208
}
192209

210+
/// A resolved attribute argument path produced by late resolution for a builtin attribute.
193211
#[derive(Debug, Copy, Clone, HashStable_Generic, Encodable, Decodable, PrintAttribute)]
194-
pub struct AttrConstResolution<Id = ast::NodeId> {
212+
pub struct AttrResolution<Id = ast::NodeId> {
213+
pub kind: AttrResolutionKind,
195214
pub path_span: Span,
196-
pub resolved: AttrConstResolved<Id>,
215+
pub resolved: AttrResolved<Id>,
197216
}
198217

199218
pub enum TransparencyError {

compiler/rustc_hir/src/attrs/pretty_printing.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol};
1717
use rustc_target::spec::SanitizerSet;
1818
use thin_vec::ThinVec;
1919

20-
use crate::attrs::AttrIntValue;
20+
use crate::HashIgnoredAttrId;
21+
use crate::attrs::{AttrIntValue, LintInstance};
2122
use crate::limit::Limit;
2223

2324
/// This trait is used to print attributes in `rustc_hir_pretty`.
@@ -205,8 +206,8 @@ macro_rules! print_tup {
205206
}
206207

207208
print_tup!(A B C D E F G H);
208-
print_skip!(Span, (), ErrorGuaranteed, AttrId);
209-
print_disp!(u8, u16, u32, u128, usize, bool, NonZero<u32>, Limit);
209+
print_skip!(Span, (), ErrorGuaranteed, AttrId, HashIgnoredAttrId);
210+
print_disp!(u8, u16, u32, u128, usize, bool, NonZero<u32>, Limit, LintInstance);
210211
print_debug!(
211212
Symbol,
212213
Ident,

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use rustc_data_structures::steal::Steal;
3737
use rustc_data_structures::unord::{UnordMap, UnordSet};
3838
use rustc_errors::{Diag, ErrorGuaranteed, LintBuffer};
3939
use rustc_hir as hir;
40-
use rustc_hir::attrs::{AttrConstResolution, StrippedCfgItem};
40+
use rustc_hir::attrs::{AttrResolution, StrippedCfgItem};
4141
use rustc_hir::def::{CtorKind, CtorOf, DefKind, DocLinkResMap, LifetimeRes, Res};
4242
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, LocalDefIdMap};
4343
use rustc_hir::{LangItem, attrs as attr, find_attr};
@@ -210,8 +210,9 @@ pub struct ResolverAstLowering<'tcx> {
210210
pub lifetimes_res_map: NodeMap<LifetimeRes>,
211211
/// Lifetime parameters that lowering will have to introduce.
212212
pub extra_lifetime_params_map: NodeMap<Vec<(Ident, ast::NodeId, LifetimeRes)>>,
213-
/// Resolutions for const item paths used in opted-in late attributes, keyed by attribute ID.
214-
pub attr_const_res_map: FxIndexMap<AttrId, Vec<AttrConstResolution<ast::NodeId>>>,
213+
/// Resolutions for builtin attribute arguments that need late name resolution, keyed by
214+
/// attribute ID.
215+
pub attr_res_map: FxIndexMap<AttrId, Vec<AttrResolution<ast::NodeId>>>,
215216

216217
pub next_node_id: ast::NodeId,
217218

compiler/rustc_resolve/src/def_collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
146146
let mut parser = AttributeParser::<'_, Early>::new(
147147
&self.resolver.tcx.sess,
148148
self.resolver.tcx.features(),
149-
Vec::new(),
149+
self.resolver.tcx().registered_tools(()),
150150
Default::default(),
151151
Early { emit_errors: ShouldEmit::Nothing },
152152
);

0 commit comments

Comments
 (0)