Skip to content

Commit eab3bb8

Browse files
committed
refactor: more general terminology and use existing attr parse machinery
1 parent be14df8 commit eab3bb8

9 files changed

Lines changed: 253 additions & 132 deletions

File tree

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
4848
use rustc_data_structures::steal::Steal;
4949
use rustc_data_structures::tagged_ptr::TaggedRef;
5050
use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle};
51-
use rustc_hir::attrs::AttrConstResolution;
51+
use rustc_hir::attrs::AttrResolution;
5252
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
5353
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
5454
use rustc_hir::definitions::{DefPathData, DisambiguatorState};
@@ -156,7 +156,7 @@ struct LoweringContext<'a, 'hir, R> {
156156
impl<'a, 'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'a, 'hir, R> {
157157
fn new(tcx: TyCtxt<'hir>, resolver: &'a mut R) -> Self {
158158
let registered_tools = tcx.registered_tools(()).iter().map(|x| x.name).collect();
159-
let attr_const_res_map = resolver.all_attr_const_resolutions();
159+
let attr_res_map = resolver.all_attr_resolutions();
160160
Self {
161161
tcx,
162162
resolver,
@@ -211,7 +211,7 @@ impl<'a, 'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'a, 'hir, R> {
211211
tcx.sess,
212212
tcx.features(),
213213
registered_tools,
214-
attr_const_res_map,
214+
attr_res_map,
215215
Late,
216216
),
217217
delayed_lints: Vec::new(),
@@ -242,7 +242,7 @@ impl SpanLowerer {
242242
struct ResolverDelayedAstLowering<'a, 'tcx> {
243243
node_id_to_def_id: NodeMap<LocalDefId>,
244244
partial_res_map: NodeMap<PartialRes>,
245-
attr_const_res_map: FxIndexMap<rustc_span::AttrId, Vec<AttrConstResolution<ast::NodeId>>>,
245+
attr_res_map: FxIndexMap<rustc_span::AttrId, Vec<AttrResolution<ast::NodeId>>>,
246246
next_node_id: NodeId,
247247
base: &'a ResolverAstLowering<'tcx>,
248248
}
@@ -257,11 +257,11 @@ impl<'a, 'tcx> ResolverAstLoweringExt<'tcx> for ResolverDelayedAstLowering<'a, '
257257
self.partial_res_map.get(&id).copied().or_else(|| self.base.get_partial_res(id))
258258
}
259259

260-
fn all_attr_const_resolutions(
260+
fn all_attr_resolutions(
261261
&self,
262-
) -> FxIndexMap<rustc_span::AttrId, Vec<AttrConstResolution<ast::NodeId>>> {
263-
let mut map = self.base.all_attr_const_resolutions();
264-
for (attr_id, resolutions) in &self.attr_const_res_map {
262+
) -> FxIndexMap<rustc_span::AttrId, Vec<AttrResolution<ast::NodeId>>> {
263+
let mut map = self.base.all_attr_resolutions();
264+
for (attr_id, resolutions) in &self.attr_res_map {
265265
map.entry(*attr_id).or_default().extend(resolutions.iter().copied());
266266
}
267267
map
@@ -359,10 +359,10 @@ impl<'tcx> ResolverAstLowering<'tcx> {
359359
self.partial_res_map.get(&id).copied()
360360
}
361361

362-
fn all_attr_const_resolutions(
362+
fn all_attr_resolutions(
363363
&self,
364-
) -> FxIndexMap<rustc_span::AttrId, Vec<AttrConstResolution<ast::NodeId>>> {
365-
self.attr_const_res_map.clone()
364+
) -> FxIndexMap<rustc_span::AttrId, Vec<AttrResolution<ast::NodeId>>> {
365+
self.attr_res_map.clone()
366366
}
367367

368368
/// Obtains per-namespace resolutions for `use` statement with the given `NodeId`.
@@ -683,7 +683,7 @@ pub fn lower_delayed_owner(tcx: TyCtxt<'_>, def_id: LocalDefId) {
683683
let mut resolver = ResolverDelayedAstLowering {
684684
next_node_id: resolver.next_node_id,
685685
partial_res_map: Default::default(),
686-
attr_const_res_map: Default::default(),
686+
attr_res_map: Default::default(),
687687
node_id_to_def_id: Default::default(),
688688
base: resolver,
689689
};

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

@@ -103,7 +103,10 @@ fn int_type_of_word(s: Symbol) -> Option<IntType> {
103103
}
104104
}
105105

106-
fn parse_repr<S: Stage>(cx: &AcceptContext<'_, '_, S>, param: &MetaItemParser) -> Option<ReprAttr> {
106+
fn parse_repr<S: Stage>(
107+
cx: &mut AcceptContext<'_, '_, S>,
108+
param: &MetaItemParser,
109+
) -> Option<ReprAttr> {
107110
use ReprAttr::*;
108111

109112
// FIXME(jdonszelmann): invert the parsing here to match on the word first and then the
@@ -198,7 +201,7 @@ enum AlignmentParseError {
198201
}
199202

200203
fn parse_repr_align<S: Stage>(
201-
cx: &AcceptContext<'_, '_, S>,
204+
cx: &mut AcceptContext<'_, '_, S>,
202205
list: &MetaItemListParser,
203206
param_span: Span,
204207
align_kind: AlignKind,
@@ -281,7 +284,7 @@ fn parse_alignment<S: Stage>(
281284
}
282285

283286
fn parse_alignment_or_const_path<S: Stage>(
284-
cx: &AcceptContext<'_, '_, S>,
287+
cx: &mut AcceptContext<'_, '_, S>,
285288
arg: &MetaItemOrLitParser,
286289
attr_name: &'static str,
287290
) -> Result<AttrIntValue, AlignmentParseError> {
@@ -299,10 +302,15 @@ fn parse_alignment_or_const_path<S: Stage>(
299302
return Err(AlignmentParseError::Message("not an unsuffixed integer".to_string()));
300303
}
301304

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

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

325335
match resolution {
326-
AttrConstResolved::Resolved(Res::Def(DefKind::Const { .. }, def_id)) => {
327-
Ok(AttrIntValue::Const { def_id, span: meta.path().span() })
336+
AttrResolved::Resolved(Res::Def(DefKind::Const { .. }, def_id)) => {
337+
Ok(AttrIntValue::Const { def_id, span: path_span })
328338
}
329-
AttrConstResolved::Resolved(Res::Def(DefKind::ConstParam, _)) => {
330-
cx.emit_err(AttrConstGenericNotSupported { span: meta.path().span(), attr_name });
339+
AttrResolved::Resolved(Res::Def(DefKind::ConstParam, _)) => {
340+
cx.emit_err(AttrConstGenericNotSupported { span: path_span, attr_name });
331341
Err(AlignmentParseError::AlreadyErrored)
332342
}
333-
AttrConstResolved::Resolved(res) => {
334-
cx.emit_err(AttrConstPathNotConst {
335-
span: meta.path().span(),
336-
attr_name,
337-
thing: res.descr(),
338-
});
343+
AttrResolved::Resolved(res) => {
344+
cx.emit_err(AttrConstPathNotConst { span: path_span, attr_name, thing: res.descr() });
339345
Err(AlignmentParseError::AlreadyErrored)
340346
}
341-
AttrConstResolved::Error => Err(AlignmentParseError::AlreadyErrored),
347+
AttrResolved::Error => Err(AlignmentParseError::AlreadyErrored),
342348
}
343349
}
344350

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use private::Sealed;
88
use rustc_ast::{AttrStyle, MetaItemLit, NodeId};
99
use rustc_errors::{Diag, Diagnostic, Level};
1010
use rustc_feature::{AttrSuggestionStyle, AttributeTemplate};
11-
use rustc_hir::attrs::AttributeKind;
11+
use rustc_hir::attrs::{AttrResolutionKind, AttributeKind};
1212
use rustc_hir::lints::AttributeLintKind;
1313
use rustc_hir::{AttrPath, HirId};
1414
use rustc_parse::parser::Recovery;
@@ -488,11 +488,25 @@ impl<'f, 'sess: 'f, S: Stage> SharedContext<'f, 'sess, S> {
488488
}
489489

490490
impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
491-
pub(crate) fn attr_const_resolution(
491+
pub(crate) fn attr_resolution(
492492
&self,
493+
kind: AttrResolutionKind,
493494
path_span: Span,
494-
) -> Option<rustc_hir::attrs::AttrConstResolved<NodeId>> {
495-
self.attr_id.and_then(|attr_id| self.shared.cx.attr_const_resolution(attr_id, path_span))
495+
) -> Option<rustc_hir::attrs::AttrResolved<NodeId>> {
496+
self.attr_id.and_then(|attr_id| self.shared.cx.attr_resolution(attr_id, kind, path_span))
497+
}
498+
499+
pub(crate) fn record_attr_resolution_request(
500+
&mut self,
501+
kind: AttrResolutionKind,
502+
path: rustc_ast::Path,
503+
) {
504+
if let Some(attr_id) = self.attr_id {
505+
self.shared.cx.record_attr_resolution_request(
506+
attr_id,
507+
crate::interface::AttrResolutionRequest { kind, path },
508+
);
509+
}
496510
}
497511

498512
fn emit_parse_error(

0 commit comments

Comments
 (0)