Skip to content

Commit e87188c

Browse files
committed
Auto merge of #87133 - GuillaumeGomez:rollup-pfz9jbk, r=GuillaumeGomez
Rollup of 6 pull requests Successful merges: - #87027 (expand: Support helper attributes for built-in derive macros) - #87056 (Fix codeblocks overflow) - #87117 (Shrink the CrateStore dynamic interface.) - #87120 (rustdoc: Remove unnecessary `extern crate` aliases) - #87125 (Fix Ayu theme `<code>` color) - #87130 (Update browser-ui-test package version) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 4f0c568 + ac6672b commit e87188c

File tree

26 files changed

+267
-174
lines changed

26 files changed

+267
-174
lines changed

compiler/rustc_builtin_macros/src/proc_macro_harness.rs

+7-76
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_ast::ptr::P;
55
use rustc_ast::visit::{self, Visitor};
66
use rustc_ast::{self as ast, NodeId};
77
use rustc_ast_pretty::pprust;
8-
use rustc_expand::base::{ExtCtxt, ResolverExpand};
8+
use rustc_expand::base::{parse_macro_name_and_helper_attrs, ExtCtxt, ResolverExpand};
99
use rustc_expand::expand::{AstFragment, ExpansionConfig};
1010
use rustc_session::Session;
1111
use rustc_span::hygiene::AstPass;
@@ -109,86 +109,17 @@ impl<'a> CollectProcMacros<'a> {
109109
}
110110

111111
fn collect_custom_derive(&mut self, item: &'a ast::Item, attr: &'a ast::Attribute) {
112-
// Once we've located the `#[proc_macro_derive]` attribute, verify
113-
// that it's of the form `#[proc_macro_derive(Foo)]` or
114-
// `#[proc_macro_derive(Foo, attributes(A, ..))]`
115-
let list = match attr.meta_item_list() {
116-
Some(list) => list,
117-
None => return,
118-
};
119-
if list.len() != 1 && list.len() != 2 {
120-
self.handler.span_err(attr.span, "attribute must have either one or two arguments");
121-
return;
122-
}
123-
let trait_attr = match list[0].meta_item() {
124-
Some(meta_item) => meta_item,
125-
_ => {
126-
self.handler.span_err(list[0].span(), "not a meta item");
127-
return;
128-
}
129-
};
130-
let trait_ident = match trait_attr.ident() {
131-
Some(trait_ident) if trait_attr.is_word() => trait_ident,
132-
_ => {
133-
self.handler.span_err(trait_attr.span, "must only be one word");
134-
return;
135-
}
136-
};
137-
138-
if !trait_ident.name.can_be_raw() {
139-
self.handler.span_err(
140-
trait_attr.span,
141-
&format!("`{}` cannot be a name of derive macro", trait_ident),
142-
);
143-
}
144-
145-
let attributes_attr = list.get(1);
146-
let proc_attrs: Vec<_> = if let Some(attr) = attributes_attr {
147-
if !attr.has_name(sym::attributes) {
148-
self.handler.span_err(attr.span(), "second argument must be `attributes`")
149-
}
150-
attr.meta_item_list()
151-
.unwrap_or_else(|| {
152-
self.handler
153-
.span_err(attr.span(), "attribute must be of form: `attributes(foo, bar)`");
154-
&[]
155-
})
156-
.iter()
157-
.filter_map(|attr| {
158-
let attr = match attr.meta_item() {
159-
Some(meta_item) => meta_item,
160-
_ => {
161-
self.handler.span_err(attr.span(), "not a meta item");
162-
return None;
163-
}
164-
};
165-
166-
let ident = match attr.ident() {
167-
Some(ident) if attr.is_word() => ident,
168-
_ => {
169-
self.handler.span_err(attr.span, "must only be one word");
170-
return None;
171-
}
172-
};
173-
if !ident.name.can_be_raw() {
174-
self.handler.span_err(
175-
attr.span,
176-
&format!("`{}` cannot be a name of derive helper attribute", ident),
177-
);
178-
}
179-
180-
Some(ident.name)
181-
})
182-
.collect()
183-
} else {
184-
Vec::new()
185-
};
112+
let (trait_name, proc_attrs) =
113+
match parse_macro_name_and_helper_attrs(self.handler, attr, "derive") {
114+
Some(name_and_attrs) => name_and_attrs,
115+
None => return,
116+
};
186117

187118
if self.in_root && item.vis.kind.is_pub() {
188119
self.macros.push(ProcMacro::Derive(ProcMacroDerive {
189120
id: item.id,
190121
span: item.span,
191-
trait_name: trait_ident.name,
122+
trait_name,
192123
function_name: item.ident,
193124
attrs: proc_attrs,
194125
}));

compiler/rustc_expand/src/base.rs

+92-2
Original file line numberDiff line numberDiff line change
@@ -745,9 +745,17 @@ impl SyntaxExtension {
745745
}
746746
}
747747

748-
let builtin_name = sess
748+
let (builtin_name, helper_attrs) = sess
749749
.find_by_name(attrs, sym::rustc_builtin_macro)
750-
.map(|a| a.value_str().unwrap_or(name));
750+
.map(|attr| {
751+
// Override `helper_attrs` passed above if it's a built-in macro,
752+
// marking `proc_macro_derive` macros as built-in is not a realistic use case.
753+
parse_macro_name_and_helper_attrs(sess.diagnostic(), attr, "built-in").map_or_else(
754+
|| (Some(name), Vec::new()),
755+
|(name, helper_attrs)| (Some(name), helper_attrs),
756+
)
757+
})
758+
.unwrap_or_else(|| (None, helper_attrs));
751759
let (stability, const_stability) = attr::find_stability(&sess, attrs, span);
752760
if let Some((_, sp)) = const_stability {
753761
sess.parse_sess
@@ -1213,6 +1221,88 @@ pub fn get_exprs_from_tts(
12131221
Some(es)
12141222
}
12151223

1224+
pub fn parse_macro_name_and_helper_attrs(
1225+
diag: &rustc_errors::Handler,
1226+
attr: &Attribute,
1227+
descr: &str,
1228+
) -> Option<(Symbol, Vec<Symbol>)> {
1229+
// Once we've located the `#[proc_macro_derive]` attribute, verify
1230+
// that it's of the form `#[proc_macro_derive(Foo)]` or
1231+
// `#[proc_macro_derive(Foo, attributes(A, ..))]`
1232+
let list = match attr.meta_item_list() {
1233+
Some(list) => list,
1234+
None => return None,
1235+
};
1236+
if list.len() != 1 && list.len() != 2 {
1237+
diag.span_err(attr.span, "attribute must have either one or two arguments");
1238+
return None;
1239+
}
1240+
let trait_attr = match list[0].meta_item() {
1241+
Some(meta_item) => meta_item,
1242+
_ => {
1243+
diag.span_err(list[0].span(), "not a meta item");
1244+
return None;
1245+
}
1246+
};
1247+
let trait_ident = match trait_attr.ident() {
1248+
Some(trait_ident) if trait_attr.is_word() => trait_ident,
1249+
_ => {
1250+
diag.span_err(trait_attr.span, "must only be one word");
1251+
return None;
1252+
}
1253+
};
1254+
1255+
if !trait_ident.name.can_be_raw() {
1256+
diag.span_err(
1257+
trait_attr.span,
1258+
&format!("`{}` cannot be a name of {} macro", trait_ident, descr),
1259+
);
1260+
}
1261+
1262+
let attributes_attr = list.get(1);
1263+
let proc_attrs: Vec<_> = if let Some(attr) = attributes_attr {
1264+
if !attr.has_name(sym::attributes) {
1265+
diag.span_err(attr.span(), "second argument must be `attributes`")
1266+
}
1267+
attr.meta_item_list()
1268+
.unwrap_or_else(|| {
1269+
diag.span_err(attr.span(), "attribute must be of form: `attributes(foo, bar)`");
1270+
&[]
1271+
})
1272+
.iter()
1273+
.filter_map(|attr| {
1274+
let attr = match attr.meta_item() {
1275+
Some(meta_item) => meta_item,
1276+
_ => {
1277+
diag.span_err(attr.span(), "not a meta item");
1278+
return None;
1279+
}
1280+
};
1281+
1282+
let ident = match attr.ident() {
1283+
Some(ident) if attr.is_word() => ident,
1284+
_ => {
1285+
diag.span_err(attr.span, "must only be one word");
1286+
return None;
1287+
}
1288+
};
1289+
if !ident.name.can_be_raw() {
1290+
diag.span_err(
1291+
attr.span,
1292+
&format!("`{}` cannot be a name of derive helper attribute", ident),
1293+
);
1294+
}
1295+
1296+
Some(ident.name)
1297+
})
1298+
.collect()
1299+
} else {
1300+
Vec::new()
1301+
};
1302+
1303+
Some((trait_ident.name, proc_attrs))
1304+
}
1305+
12161306
/// This nonterminal looks like some specific enums from
12171307
/// `proc-macro-hack` and `procedural-masquerade` crates.
12181308
/// We need to maintain some special pretty-printing behavior for them due to incorrect

compiler/rustc_feature/src/builtin_attrs.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,11 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
448448
// Internal attributes, Macro related:
449449
// ==========================================================================
450450

451-
rustc_attr!(rustc_builtin_macro, AssumedUsed, template!(Word, NameValueStr: "name"), IMPL_DETAIL),
451+
rustc_attr!(
452+
rustc_builtin_macro, AssumedUsed,
453+
template!(Word, List: "name, /*opt*/ attributes(name1, name2, ...)"),
454+
IMPL_DETAIL,
455+
),
452456
rustc_attr!(rustc_proc_macro_decls, Normal, template!(Word), INTERNAL_UNSTABLE),
453457
rustc_attr!(
454458
rustc_macro_transparency, AssumedUsed,

compiler/rustc_interface/src/passes.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_metadata::creader::CStore;
1818
use rustc_middle::arena::Arena;
1919
use rustc_middle::dep_graph::DepGraph;
2020
use rustc_middle::middle;
21-
use rustc_middle::middle::cstore::{CrateStore, MetadataLoader, MetadataLoaderDyn};
21+
use rustc_middle::middle::cstore::{MetadataLoader, MetadataLoaderDyn};
2222
use rustc_middle::ty::query::Providers;
2323
use rustc_middle::ty::{self, GlobalCtxt, ResolverOutputs, TyCtxt};
2424
use rustc_mir as mir;
@@ -860,11 +860,7 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
860860
tcx.ensure().proc_macro_decls_static(())
861861
});
862862

863-
let cstore = tcx
864-
.cstore_as_any()
865-
.downcast_ref::<CStore>()
866-
.expect("`tcx.cstore` is not a `CStore`");
867-
cstore.report_unused_deps(tcx);
863+
CStore::from_tcx(tcx).report_unused_deps(tcx);
868864
},
869865
{
870866
par_iter(&tcx.hir().krate().modules).for_each(|(&module, _)| {

compiler/rustc_metadata/src/creader.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl<'a> std::fmt::Debug for CrateDump<'a> {
130130
}
131131

132132
impl CStore {
133-
crate fn from_tcx(tcx: TyCtxt<'_>) -> &CStore {
133+
pub fn from_tcx(tcx: TyCtxt<'_>) -> &CStore {
134134
tcx.cstore_as_any().downcast_ref::<CStore>().expect("`tcx.cstore` is not a `CStore`")
135135
}
136136

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+13-17
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::rmeta::encoder;
55

66
use rustc_ast as ast;
77
use rustc_data_structures::stable_map::FxHashMap;
8-
use rustc_data_structures::svh::Svh;
98
use rustc_hir as hir;
109
use rustc_hir::def::{CtorKind, DefKind};
1110
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, CRATE_DEF_INDEX, LOCAL_CRATE};
@@ -369,6 +368,7 @@ pub fn provide(providers: &mut Providers) {
369368
tcx.arena
370369
.alloc_slice(&CStore::from_tcx(tcx).crate_dependencies_in_postorder(LOCAL_CRATE))
371370
},
371+
crates: |tcx, ()| tcx.arena.alloc_slice(&CStore::from_tcx(tcx).crates_untracked()),
372372

373373
..*providers
374374
};
@@ -451,6 +451,16 @@ impl CStore {
451451
self.get_crate_data(def_id.krate).get_span(def_id.index, sess)
452452
}
453453

454+
pub fn def_kind(&self, def: DefId) -> DefKind {
455+
self.get_crate_data(def.krate).def_kind(def.index)
456+
}
457+
458+
pub fn crates_untracked(&self) -> Vec<CrateNum> {
459+
let mut result = vec![];
460+
self.iter_crate_data(|cnum, _| result.push(cnum));
461+
result
462+
}
463+
454464
pub fn item_generics_num_lifetimes(&self, def_id: DefId, sess: &Session) -> usize {
455465
self.get_crate_data(def_id.krate).get_generics(def_id.index, sess).own_counts().lifetimes
456466
}
@@ -485,29 +495,21 @@ impl CrateStore for CStore {
485495
self
486496
}
487497

488-
fn crate_name_untracked(&self, cnum: CrateNum) -> Symbol {
498+
fn crate_name(&self, cnum: CrateNum) -> Symbol {
489499
self.get_crate_data(cnum).root.name
490500
}
491501

492-
fn stable_crate_id_untracked(&self, cnum: CrateNum) -> StableCrateId {
502+
fn stable_crate_id(&self, cnum: CrateNum) -> StableCrateId {
493503
self.get_crate_data(cnum).root.stable_crate_id
494504
}
495505

496-
fn crate_hash_untracked(&self, cnum: CrateNum) -> Svh {
497-
self.get_crate_data(cnum).root.hash
498-
}
499-
500506
/// Returns the `DefKey` for a given `DefId`. This indicates the
501507
/// parent `DefId` as well as some idea of what kind of data the
502508
/// `DefId` refers to.
503509
fn def_key(&self, def: DefId) -> DefKey {
504510
self.get_crate_data(def.krate).def_key(def.index)
505511
}
506512

507-
fn def_kind(&self, def: DefId) -> DefKind {
508-
self.get_crate_data(def.krate).def_kind(def.index)
509-
}
510-
511513
fn def_path(&self, def: DefId) -> DefPath {
512514
self.get_crate_data(def.krate).def_path(def.index)
513515
}
@@ -526,12 +528,6 @@ impl CrateStore for CStore {
526528
self.get_crate_data(cnum).def_path_hash_to_def_id(cnum, index_guess, hash)
527529
}
528530

529-
fn crates_untracked(&self) -> Vec<CrateNum> {
530-
let mut result = vec![];
531-
self.iter_crate_data(|cnum, _| result.push(cnum));
532-
result
533-
}
534-
535531
fn encode_metadata(&self, tcx: TyCtxt<'_>) -> EncodedMetadata {
536532
encoder::encode_metadata(tcx)
537533
}

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1061,10 +1061,7 @@ impl EncodeContext<'a, 'tcx> {
10611061
Lazy::empty()
10621062
};
10631063

1064-
let data = ModData {
1065-
reexports,
1066-
expansion: tcx.resolutions(()).definitions.expansion_that_defined(local_def_id),
1067-
};
1064+
let data = ModData { reexports, expansion: tcx.expn_that_defined(local_def_id) };
10681065

10691066
record!(self.tables.kind[def_id] <- EntryKind::Mod(self.lazy(data)));
10701067
if self.is_proc_macro {

compiler/rustc_middle/src/hir/map/mod.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use self::collector::NodeCollector;
22

33
use crate::hir::{AttributeMap, IndexedHir};
4-
use crate::middle::cstore::CrateStore;
54
use crate::ty::TyCtxt;
65
use rustc_ast as ast;
76
use rustc_data_structures::fingerprint::Fingerprint;
@@ -991,7 +990,7 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
991990
},
992991
);
993992

994-
let upstream_crates = upstream_crates(&*tcx.untracked_resolutions.cstore);
993+
let upstream_crates = upstream_crates(tcx);
995994

996995
// We hash the final, remapped names of all local source files so we
997996
// don't have to include the path prefix remapping commandline args.
@@ -1021,13 +1020,13 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
10211020
Svh::new(crate_hash.to_smaller_hash())
10221021
}
10231022

1024-
fn upstream_crates(cstore: &dyn CrateStore) -> Vec<(StableCrateId, Svh)> {
1025-
let mut upstream_crates: Vec<_> = cstore
1026-
.crates_untracked()
1023+
fn upstream_crates(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> {
1024+
let mut upstream_crates: Vec<_> = tcx
1025+
.crates(())
10271026
.iter()
10281027
.map(|&cnum| {
1029-
let stable_crate_id = cstore.stable_crate_id_untracked(cnum);
1030-
let hash = cstore.crate_hash_untracked(cnum);
1028+
let stable_crate_id = tcx.resolutions(()).cstore.stable_crate_id(cnum);
1029+
let hash = tcx.crate_hash(cnum);
10311030
(stable_crate_id, hash)
10321031
})
10331032
.collect();

0 commit comments

Comments
 (0)