Skip to content

Commit 4bf7faf

Browse files
committed
Auto merge of #156303 - JonathanBrouwer:rollup-E43iihK, r=JonathanBrouwer
Rollup of 3 pull requests Successful merges: - #156236 (resolve: Remove `MacroData`) - #156298 (Rename the unstable integer `extend` function to `widen`) - #156214 (Do not cache `lints_that_dont_need_to_run` across sessions)
2 parents 3e353d7 + 04f8cc8 commit 4bf7faf

14 files changed

Lines changed: 132 additions & 138 deletions

File tree

compiler/rustc_expand/src/mbe/macro_rules.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ pub struct MacroRulesMacroExpander {
170170
transparency: Transparency,
171171
kinds: MacroKinds,
172172
rules: Vec<MacroRule>,
173+
macro_rules: bool,
173174
}
174175

175176
impl MacroRulesMacroExpander {
@@ -189,6 +190,14 @@ impl MacroRulesMacroExpander {
189190
self.kinds
190191
}
191192

193+
pub fn nrules(&self) -> usize {
194+
self.rules.len()
195+
}
196+
197+
pub fn is_macro_rules(&self) -> bool {
198+
self.macro_rules
199+
}
200+
192201
pub fn expand_derive(
193202
&self,
194203
cx: &mut ExtCtxt<'_>,
@@ -714,13 +723,12 @@ pub fn compile_declarative_macro(
714723
span: Span,
715724
node_id: NodeId,
716725
edition: Edition,
717-
) -> (SyntaxExtension, usize) {
726+
) -> SyntaxExtension {
718727
let mk_syn_ext = |kind| {
719728
let is_local = is_defined_in_current_crate(node_id);
720729
SyntaxExtension::new(sess, kind, span, Vec::new(), edition, ident.name, attrs, is_local)
721730
};
722-
let dummy_syn_ext =
723-
|guar| (mk_syn_ext(SyntaxExtensionKind::Bang(Arc::new(DummyBang(guar)))), 0);
731+
let dummy_syn_ext = |guar| mk_syn_ext(SyntaxExtensionKind::Bang(Arc::new(DummyBang(guar))));
724732

725733
let macro_rules = macro_def.macro_rules;
726734
let exp_sep = if macro_rules { exp!(Semi) } else { exp!(Comma) };
@@ -857,9 +865,6 @@ pub fn compile_declarative_macro(
857865
return dummy_syn_ext(guar);
858866
}
859867

860-
// Return the number of rules for unused rule linting, if this is a local macro.
861-
let nrules = if is_defined_in_current_crate(node_id) { rules.len() } else { 0 };
862-
863868
let on_unmatch_args = find_attr!(
864869
attrs,
865870
OnUnmatchArgs { directive, .. } => directive.clone()
@@ -875,8 +880,9 @@ pub fn compile_declarative_macro(
875880
on_unmatch_args,
876881
transparency,
877882
rules,
883+
macro_rules,
878884
};
879-
(mk_syn_ext(SyntaxExtensionKind::MacroRules(Arc::new(exp))), nrules)
885+
mk_syn_ext(SyntaxExtensionKind::MacroRules(Arc::new(exp)))
880886
}
881887

882888
fn check_no_eof(sess: &Session, p: &Parser<'_>, msg: &'static str) -> Option<ErrorGuaranteed> {

compiler/rustc_middle/src/queries.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,9 @@ rustc_queries! {
565565

566566
query lints_that_dont_need_to_run(_: ()) -> &'tcx UnordSet<LintId> {
567567
arena_cache
568+
// This depends on the lint store, which includes internal lints when the
569+
// untracked `-Zunstable-options` flag is set.
570+
eval_always
568571
desc { "Computing all lints that are explicitly enabled or with a default level greater than Allow" }
569572
}
570573

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_ast::{
1414
TyAlias,
1515
};
1616
use rustc_attr_parsing::AttributeParser;
17-
use rustc_expand::base::ResolverExpand;
17+
use rustc_expand::base::{ResolverExpand, SyntaxExtension, SyntaxExtensionKind};
1818
use rustc_hir::Attribute;
1919
use rustc_hir::attrs::{AttributeKind, MacroUseArgs};
2020
use rustc_hir::def::{self, *};
@@ -37,9 +37,8 @@ use crate::macros::{MacroRulesDecl, MacroRulesScope, MacroRulesScopeRef};
3737
use crate::ref_mut::CmCell;
3838
use crate::{
3939
BindingKey, Decl, DeclData, DeclKind, DelayedVisResolutionError, ExternModule,
40-
ExternPreludeEntry, Finalize, IdentKey, LocalModule, MacroData, Module, ModuleKind,
41-
ModuleOrUniformRoot, ParentScope, PathResult, Res, Resolver, Segment, SyntaxExtension, Used,
42-
VisResolutionError, errors,
40+
ExternPreludeEntry, Finalize, IdentKey, LocalModule, Module, ModuleKind, ModuleOrUniformRoot,
41+
ParentScope, PathResult, Res, Resolver, Segment, Used, VisResolutionError, errors,
4342
};
4443

4544
impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
@@ -208,28 +207,28 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
208207
}
209208

210209
/// Gets the `SyntaxExtension` corresponding to `res`.
211-
pub(crate) fn get_macro(&self, res: Res) -> Option<&Arc<SyntaxExtension>> {
210+
pub(crate) fn get_macro(&self, res: Res) -> Option<&'ra Arc<SyntaxExtension>> {
212211
match res {
213-
Res::Def(DefKind::Macro(..), def_id) => Some(&self.get_macro_by_def_id(def_id).ext),
214-
Res::NonMacroAttr(_) => Some(&self.non_macro_attr),
212+
Res::Def(DefKind::Macro(..), def_id) => Some(self.get_macro_by_def_id(def_id)),
213+
Res::NonMacroAttr(_) => Some(self.non_macro_attr),
215214
_ => None,
216215
}
217216
}
218217

219-
pub(crate) fn get_macro_by_def_id(&self, def_id: DefId) -> &'ra MacroData {
218+
pub(crate) fn get_macro_by_def_id(&self, def_id: DefId) -> &'ra Arc<SyntaxExtension> {
220219
// Local macros are always compiled.
221220
match def_id.as_local() {
222221
Some(local_def_id) => self.local_macro_map[&local_def_id],
223-
None => *self.extern_macro_map.borrow_mut().entry(def_id).or_insert_with(|| {
222+
None => self.extern_macro_map.borrow_mut().entry(def_id).or_insert_with(|| {
224223
let loaded_macro = self.cstore().load_macro_untracked(self.tcx, def_id);
225-
let macro_data = match loaded_macro {
224+
let ext = match loaded_macro {
226225
LoadedMacro::MacroDef { def, ident, attrs, span, edition } => {
227226
self.compile_macro(&def, ident, &attrs, span, ast::DUMMY_NODE_ID, edition)
228227
}
229-
LoadedMacro::ProcMacro(ext) => MacroData::new(Arc::new(ext)),
228+
LoadedMacro::ProcMacro(ext) => ext,
230229
};
231230

232-
self.arenas.alloc_macro(macro_data)
231+
self.arenas.alloc_macro(ext)
233232
}),
234233
}
235234
}
@@ -1277,8 +1276,10 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
12771276
fn insert_unused_macro(&mut self, ident: Ident, def_id: LocalDefId, node_id: NodeId) {
12781277
if !ident.as_str().starts_with('_') {
12791278
self.r.unused_macros.insert(def_id, (node_id, ident));
1280-
let nrules = self.r.local_macro_map[&def_id].nrules;
1281-
self.r.unused_macro_rules.insert(node_id, (def_id, DenseBitSet::new_filled(nrules)));
1279+
if let SyntaxExtensionKind::MacroRules(mr) = &self.r.local_macro_map[&def_id].kind {
1280+
let value = (def_id, DenseBitSet::new_filled(mr.nrules()));
1281+
self.r.unused_macro_rules.insert(node_id, value);
1282+
}
12821283
}
12831284
}
12841285

@@ -1299,8 +1300,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
12991300
Some((macro_kind, ident, span)) => {
13001301
let macro_kinds = macro_kind.into();
13011302
let res = Res::Def(DefKind::Macro(macro_kinds), def_id.to_def_id());
1302-
let macro_data = MacroData::new(self.r.dummy_ext(macro_kind));
1303-
self.r.new_local_macro(def_id, macro_data);
1303+
self.r.local_macro_map.insert(def_id, self.r.dummy_ext(macro_kind));
13041304
self.r.proc_macro_stubs.insert(def_id);
13051305
(res, ident, span, false)
13061306
}

compiler/rustc_resolve/src/def_collector.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
117117
fn visit_item(&mut self, i: &'a Item) {
118118
// Pick the def data. This need not be unique, but the more
119119
// information we encapsulate into, the better
120-
let mut opt_macro_data = None;
120+
let mut opt_syn_ext = None;
121121
let def_kind = match &i.kind {
122122
ItemKind::Impl(i) => DefKind::Impl { of_trait: i.of_trait.is_some() },
123123
ItemKind::ForeignMod(..) => DefKind::ForeignMod,
@@ -165,9 +165,9 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
165165
},
166166
);
167167

168-
let macro_data = self.r.compile_macro(def, *ident, &attrs, i.span, i.id, edition);
169-
let macro_kinds = macro_data.ext.macro_kinds();
170-
opt_macro_data = Some(macro_data);
168+
let ext = self.r.compile_macro(def, *ident, &attrs, i.span, i.id, edition);
169+
let macro_kinds = ext.macro_kinds();
170+
opt_syn_ext = Some(ext);
171171
DefKind::Macro(macro_kinds)
172172
}
173173
ItemKind::GlobalAsm(..) => DefKind::GlobalAsm,
@@ -185,8 +185,8 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
185185
};
186186
let feed = self.create_def(i.id, i.kind.ident().map(|ident| ident.name), def_kind, i.span);
187187

188-
if let Some(macro_data) = opt_macro_data {
189-
self.r.new_local_macro(feed.def_id(), macro_data);
188+
if let Some(ext) = opt_syn_ext {
189+
self.r.local_macro_map.insert(feed.def_id(), self.r.arenas.alloc_macro(ext));
190190
}
191191

192192
self.with_parent(feed.def_id(), |this| {

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,7 +1742,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17421742
if let Some((def_id, unused_ident)) = unused_macro {
17431743
let scope = self.local_macro_def_scopes[&def_id];
17441744
let parent_nearest = parent_scope.module.nearest_parent_mod();
1745-
let unused_macro_kinds = self.local_macro_map[def_id].ext.macro_kinds();
1745+
let unused_macro_kinds = self.local_macro_map[def_id].macro_kinds();
17461746
if !unused_macro_kinds.contains(macro_kind.into()) {
17471747
match macro_kind {
17481748
MacroKind::Bang => {
@@ -1860,13 +1860,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18601860
let mut all_attrs: UnordMap<Symbol, Vec<_>> = UnordMap::default();
18611861
// We're collecting these in a hashmap, and handle ordering the output further down.
18621862
#[allow(rustc::potential_query_instability)]
1863-
for (def_id, data) in self
1863+
for (def_id, ext) in self
18641864
.local_macro_map
18651865
.iter()
1866-
.map(|(local_id, data)| (local_id.to_def_id(), data))
1866+
.map(|(local_id, ext)| (local_id.to_def_id(), ext))
18671867
.chain(self.extern_macro_map.borrow().iter().map(|(id, d)| (*id, d)))
18681868
{
1869-
for helper_attr in &data.ext.helper_attrs {
1869+
for helper_attr in &ext.helper_attrs {
18701870
let item_name = self.tcx.item_name(def_id);
18711871
all_attrs.entry(*helper_attr).or_default().push(item_name);
18721872
if helper_attr == &ident.name {

compiler/rustc_resolve/src/ident.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
271271
// The macro is a proc macro derive
272272
&& let Some(def_id) = module.expansion.expn_data().macro_def_id
273273
{
274-
let ext = &self.get_macro_by_def_id(def_id).ext;
274+
let ext = self.get_macro_by_def_id(def_id);
275275
if ext.builtin_name.is_none()
276276
&& ext.macro_kinds() == MacroKinds::DERIVE
277277
&& parent.expansion.outer_expn_is_descendant_of(**ctxt)

compiler/rustc_resolve/src/imports.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_errors::codes::*;
1212
use rustc_errors::{
1313
Applicability, BufferedEarlyLint, Diagnostic, MultiSpan, pluralize, struct_span_code_err,
1414
};
15+
use rustc_expand::base::SyntaxExtensionKind;
1516
use rustc_hir::Attribute;
1617
use rustc_hir::attrs::AttributeKind;
1718
use rustc_hir::attrs::diagnostic::{CustomDiagnostic, Directive, FormatArgs};
@@ -1656,7 +1657,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16561657
match decl.kind {
16571658
// exclude decl_macro
16581659
DeclKind::Def(Res::Def(DefKind::Macro(_), def_id))
1659-
if self.get_macro_by_def_id(def_id).macro_rules =>
1660+
if let SyntaxExtensionKind::MacroRules(mr) =
1661+
&self.get_macro_by_def_id(def_id).kind
1662+
&& mr.is_macro_rules() =>
16601663
{
16611664
err.subdiagnostic(ConsiderAddingMacroExport { span: decl.span });
16621665
err.subdiagnostic(ConsiderMarkingAsPubCrate { vis_span: import.vis_span });

compiler/rustc_resolve/src/lib.rs

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,18 +1265,6 @@ struct DeriveData {
12651265
has_derive_copy: bool,
12661266
}
12671267

1268-
struct MacroData {
1269-
ext: Arc<SyntaxExtension>,
1270-
nrules: usize,
1271-
macro_rules: bool,
1272-
}
1273-
1274-
impl MacroData {
1275-
fn new(ext: Arc<SyntaxExtension>) -> MacroData {
1276-
MacroData { ext, nrules: 0, macro_rules: false }
1277-
}
1278-
}
1279-
12801268
pub struct ResolverOutputs<'tcx> {
12811269
pub global_ctxt: ResolverGlobalCtxt,
12821270
pub ast_lowering: ResolverAstLowering<'tcx>,
@@ -1396,12 +1384,12 @@ pub struct Resolver<'ra, 'tcx> {
13961384
registered_tools: &'tcx RegisteredTools,
13971385
macro_use_prelude: FxIndexMap<Symbol, Decl<'ra>>,
13981386
/// Eagerly populated map of all local macro definitions.
1399-
local_macro_map: FxHashMap<LocalDefId, &'ra MacroData> = default::fx_hash_map(),
1387+
local_macro_map: FxHashMap<LocalDefId, &'ra Arc<SyntaxExtension>> = default::fx_hash_map(),
14001388
/// Lazily populated cache of macro definitions loaded from external crates.
1401-
extern_macro_map: CacheRefCell<FxHashMap<DefId, &'ra MacroData>>,
1402-
dummy_ext_bang: Arc<SyntaxExtension>,
1403-
dummy_ext_derive: Arc<SyntaxExtension>,
1404-
non_macro_attr: Arc<SyntaxExtension>,
1389+
extern_macro_map: CacheRefCell<FxHashMap<DefId, &'ra Arc<SyntaxExtension>>>,
1390+
dummy_ext_bang: &'ra Arc<SyntaxExtension>,
1391+
dummy_ext_derive: &'ra Arc<SyntaxExtension>,
1392+
non_macro_attr: &'ra Arc<SyntaxExtension>,
14051393
local_macro_def_scopes: FxHashMap<LocalDefId, LocalModule<'ra>> = default::fx_hash_map(),
14061394
ast_transform_scopes: FxHashMap<LocalExpnId, LocalModule<'ra>> = default::fx_hash_map(),
14071395
unused_macros: FxIndexMap<LocalDefId, (NodeId, Ident)>,
@@ -1520,7 +1508,7 @@ pub struct ResolverArenas<'ra> {
15201508
imports: TypedArena<ImportData<'ra>>,
15211509
name_resolutions: TypedArena<CmRefCell<NameResolution<'ra>>>,
15221510
ast_paths: TypedArena<ast::Path>,
1523-
macros: TypedArena<MacroData>,
1511+
macros: TypedArena<Arc<SyntaxExtension>>,
15241512
dropless: DroplessArena,
15251513
}
15261514

@@ -1599,8 +1587,8 @@ impl<'ra> ResolverArenas<'ra> {
15991587
fn alloc_ast_paths(&'ra self, paths: &[ast::Path]) -> &'ra [ast::Path] {
16001588
self.ast_paths.alloc_from_iter(paths.iter().cloned())
16011589
}
1602-
fn alloc_macro(&'ra self, macro_data: MacroData) -> &'ra MacroData {
1603-
self.macros.alloc(macro_data)
1590+
fn alloc_macro(&'ra self, ext: SyntaxExtension) -> &'ra Arc<SyntaxExtension> {
1591+
self.macros.alloc(Arc::new(ext))
16041592
}
16051593
fn alloc_pattern_spans(&'ra self, spans: impl Iterator<Item = Span>) -> &'ra [Span] {
16061594
self.dropless.alloc_from_iter(spans)
@@ -1819,9 +1807,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18191807
registered_tools,
18201808
macro_use_prelude: Default::default(),
18211809
extern_macro_map: Default::default(),
1822-
dummy_ext_bang: Arc::new(SyntaxExtension::dummy_bang(edition)),
1823-
dummy_ext_derive: Arc::new(SyntaxExtension::dummy_derive(edition)),
1824-
non_macro_attr: Arc::new(SyntaxExtension::non_macro_attr(edition)),
1810+
dummy_ext_bang: arenas.alloc_macro(SyntaxExtension::dummy_bang(edition)),
1811+
dummy_ext_derive: arenas.alloc_macro(SyntaxExtension::dummy_derive(edition)),
1812+
non_macro_attr: arenas.alloc_macro(SyntaxExtension::non_macro_attr(edition)),
18251813
unused_macros: Default::default(),
18261814
unused_macro_rules: Default::default(),
18271815
single_segment_macro_resolutions: Default::default(),
@@ -1888,12 +1876,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18881876
module
18891877
}
18901878

1891-
fn new_local_macro(&mut self, def_id: LocalDefId, macro_data: MacroData) -> &'ra MacroData {
1892-
let mac = self.arenas.alloc_macro(macro_data);
1893-
self.local_macro_map.insert(def_id, mac);
1894-
mac
1895-
}
1896-
18971879
fn next_node_id(&mut self) -> NodeId {
18981880
let start = self.next_node_id;
18991881
let next = start.as_u32().checked_add(1).expect("input too large; ran out of NodeIds");
@@ -1988,11 +1970,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
19881970
CStore::from_tcx_mut(self.tcx)
19891971
}
19901972

1991-
fn dummy_ext(&self, macro_kind: MacroKind) -> Arc<SyntaxExtension> {
1973+
fn dummy_ext(&self, macro_kind: MacroKind) -> &'ra Arc<SyntaxExtension> {
19921974
match macro_kind {
1993-
MacroKind::Bang => Arc::clone(&self.dummy_ext_bang),
1994-
MacroKind::Derive => Arc::clone(&self.dummy_ext_derive),
1995-
MacroKind::Attr => Arc::clone(&self.non_macro_attr),
1975+
MacroKind::Bang => self.dummy_ext_bang,
1976+
MacroKind::Derive => self.dummy_ext_derive,
1977+
MacroKind::Attr => self.non_macro_attr,
19961978
}
19971979
}
19981980

0 commit comments

Comments
 (0)