Skip to content

Commit 840245e

Browse files
Auto merge of #150981 - matthiaskrgr:rollup-px5fFnj, r=matthiaskrgr
Rollup of 9 pull requests Successful merges: - #147938 (Add const cloning of slices and tests) - #149718 (Add freeze file times on Windows) - #150438 (Remove mentions of debootstrap and chroots from the m68k-unknown-none-elf platform support doc) - #150790 (feat: invisible character help string) - #150906 (Simplify `#[eii]` macro using methods on ecx) - #150938 (Port `#[collapse_debuginfo]` to the new attribute parsing system) - #150953 (std: sys: fs: uefi: Implement copy) - #150964 (Completely list all unparsed attributes) - #150975 (ui: add test for normalizing const projections with assoc const equality) Failed merges: - #150972 (Rename EII attributes slightly (being consistent in naming things foreign items, not extern items)) r? @ghost
2 parents 44a5b55 + c31e68f commit 840245e

File tree

30 files changed

+539
-464
lines changed

30 files changed

+539
-464
lines changed

‎compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_hir::attrs::MacroUseArgs;
1+
use rustc_hir::attrs::{CollapseMacroDebuginfo, MacroUseArgs};
22
use rustc_session::lint::builtin::INVALID_MACRO_EXPORT_ARGUMENTS;
33

44
use super::prelude::*;
@@ -163,3 +163,46 @@ impl<S: Stage> SingleAttributeParser<S> for MacroExportParser {
163163
Some(AttributeKind::MacroExport { span: cx.attr_span, local_inner_macros })
164164
}
165165
}
166+
167+
pub(crate) struct CollapseDebugInfoParser;
168+
169+
impl<S: Stage> SingleAttributeParser<S> for CollapseDebugInfoParser {
170+
const PATH: &[Symbol] = &[sym::collapse_debuginfo];
171+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
172+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
173+
const TEMPLATE: AttributeTemplate = template!(
174+
List: &["no", "external", "yes"],
175+
"https://doc.rust-lang.org/reference/attributes/debugger.html#the-collapse_debuginfo-attribute"
176+
);
177+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::MacroDef)]);
178+
179+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
180+
let Some(list) = args.list() else {
181+
cx.expected_list(cx.attr_span, args);
182+
return None;
183+
};
184+
let Some(single) = list.single() else {
185+
cx.expected_single_argument(list.span);
186+
return None;
187+
};
188+
let Some(mi) = single.meta_item() else {
189+
cx.unexpected_literal(single.span());
190+
return None;
191+
};
192+
if let Err(err) = mi.args().no_args() {
193+
cx.expected_no_args(err);
194+
}
195+
let path = mi.path().word_sym();
196+
let info = match path {
197+
Some(sym::yes) => CollapseMacroDebuginfo::Yes,
198+
Some(sym::no) => CollapseMacroDebuginfo::No,
199+
Some(sym::external) => CollapseMacroDebuginfo::External,
200+
_ => {
201+
cx.expected_specific_argument(mi.span(), &[sym::yes, sym::no, sym::external]);
202+
return None;
203+
}
204+
};
205+
206+
Some(AttributeKind::CollapseDebugInfo(info))
207+
}
208+
}

‎compiler/rustc_attr_parsing/src/context.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ use crate::attributes::lint_helpers::{
4848
};
4949
use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
5050
use crate::attributes::macro_attrs::{
51-
AllowInternalUnsafeParser, MacroEscapeParser, MacroExportParser, MacroUseParser,
51+
AllowInternalUnsafeParser, CollapseDebugInfoParser, MacroEscapeParser, MacroExportParser,
52+
MacroUseParser,
5253
};
5354
use crate::attributes::must_use::MustUseParser;
5455
use crate::attributes::no_implicit_prelude::NoImplicitPreludeParser;
@@ -191,6 +192,7 @@ attribute_parsers!(
191192

192193
// tidy-alphabetical-start
193194
Single<CfiEncodingParser>,
195+
Single<CollapseDebugInfoParser>,
194196
Single<CoverageParser>,
195197
Single<CrateNameParser>,
196198
Single<CustomMirParser>,

‎compiler/rustc_attr_parsing/src/interface.rs‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,17 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
442442

443443
/// Returns whether there is a parser for an attribute with this name
444444
pub fn is_parsed_attribute(path: &[Symbol]) -> bool {
445-
Late::parsers().accepters.contains_key(path) || EARLY_PARSED_ATTRIBUTES.contains(&path)
445+
/// The list of attributes that are parsed attributes,
446+
/// even though they don't have a parser in `Late::parsers()`
447+
const SPECIAL_ATTRIBUTES: &[&[Symbol]] = &[
448+
// Cfg attrs are removed after being early-parsed, so don't need to be in the parser list
449+
&[sym::cfg],
450+
&[sym::cfg_attr],
451+
];
452+
453+
Late::parsers().accepters.contains_key(path)
454+
|| EARLY_PARSED_ATTRIBUTES.contains(&path)
455+
|| SPECIAL_ATTRIBUTES.contains(&path)
446456
}
447457

448458
fn lower_attr_args(&self, args: &ast::AttrArgs, lower_span: impl Fn(Span) -> Span) -> AttrArgs {

‎compiler/rustc_builtin_macros/src/eii.rs‎

Lines changed: 78 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -108,33 +108,33 @@ fn eii_(
108108
let mut return_items = Vec::new();
109109

110110
if func.body.is_some() {
111-
return_items.push(Box::new(generate_default_impl(
111+
return_items.push(generate_default_impl(
112112
ecx,
113113
&func,
114114
impl_unsafe,
115115
macro_name,
116116
eii_attr_span,
117117
item_span,
118118
foreign_item_name,
119-
)))
119+
))
120120
}
121121

122-
return_items.push(Box::new(generate_foreign_item(
122+
return_items.push(generate_foreign_item(
123123
ecx,
124124
eii_attr_span,
125125
item_span,
126126
func,
127127
vis,
128128
&attrs_from_decl,
129-
)));
130-
return_items.push(Box::new(generate_attribute_macro_to_implement(
129+
));
130+
return_items.push(generate_attribute_macro_to_implement(
131131
ecx,
132132
eii_attr_span,
133133
macro_name,
134134
foreign_item_name,
135135
impl_unsafe,
136136
&attrs_from_decl,
137-
)));
137+
));
138138

139139
return_items.into_iter().map(wrap_item).collect()
140140
}
@@ -194,7 +194,7 @@ fn generate_default_impl(
194194
eii_attr_span: Span,
195195
item_span: Span,
196196
foreign_item_name: Ident,
197-
) -> ast::Item {
197+
) -> Box<ast::Item> {
198198
// FIXME: re-add some original attrs
199199
let attrs = ThinVec::new();
200200

@@ -211,124 +211,72 @@ fn generate_default_impl(
211211
span: eii_attr_span,
212212
is_default: true,
213213
known_eii_macro_resolution: Some(ast::EiiExternTarget {
214-
extern_item_path: ast::Path {
215-
span: foreign_item_name.span,
216-
segments: thin_vec![
217-
ast::PathSegment {
218-
ident: Ident::from_str_and_span("super", foreign_item_name.span,),
219-
id: DUMMY_NODE_ID,
220-
args: None
221-
},
222-
ast::PathSegment { ident: foreign_item_name, id: DUMMY_NODE_ID, args: None },
223-
],
224-
tokens: None,
225-
},
214+
extern_item_path: ecx.path(
215+
foreign_item_name.span,
216+
// prefix super to escape the `dflt` module generated below
217+
vec![Ident::from_str_and_span("super", foreign_item_name.span), foreign_item_name],
218+
),
226219
impl_unsafe,
227220
}),
228221
});
229222

230-
ast::Item {
231-
attrs: ThinVec::new(),
232-
id: ast::DUMMY_NODE_ID,
233-
span: eii_attr_span,
234-
vis: ast::Visibility {
235-
span: eii_attr_span,
236-
kind: ast::VisibilityKind::Inherited,
237-
tokens: None,
238-
},
239-
kind: ast::ItemKind::Const(Box::new(ast::ConstItem {
240-
ident: Ident { name: kw::Underscore, span: eii_attr_span },
241-
defaultness: ast::Defaultness::Final,
242-
generics: ast::Generics::default(),
243-
ty: Box::new(ast::Ty {
244-
id: DUMMY_NODE_ID,
245-
kind: ast::TyKind::Tup(ThinVec::new()),
246-
span: eii_attr_span,
247-
tokens: None,
248-
}),
249-
rhs: Some(ast::ConstItemRhs::Body(Box::new(ast::Expr {
250-
id: DUMMY_NODE_ID,
251-
kind: ast::ExprKind::Block(
252-
Box::new(ast::Block {
253-
stmts: thin_vec![ast::Stmt {
254-
id: DUMMY_NODE_ID,
255-
kind: ast::StmtKind::Item(Box::new(ast::Item {
256-
attrs: ThinVec::new(),
257-
id: DUMMY_NODE_ID,
258-
span: item_span,
259-
vis: ast::Visibility {
260-
span: item_span,
261-
kind: ast::VisibilityKind::Inherited,
262-
tokens: None
263-
},
264-
kind: ItemKind::Mod(
265-
ast::Safety::Default,
266-
Ident::from_str_and_span("dflt", item_span),
267-
ast::ModKind::Loaded(
268-
thin_vec![
269-
Box::new(ast::Item {
270-
attrs: thin_vec![ecx.attr_nested_word(
271-
sym::allow,
272-
sym::unused_imports,
273-
item_span
274-
),],
275-
id: DUMMY_NODE_ID,
276-
span: item_span,
277-
vis: ast::Visibility {
278-
span: eii_attr_span,
279-
kind: ast::VisibilityKind::Inherited,
280-
tokens: None
281-
},
282-
kind: ItemKind::Use(ast::UseTree {
283-
prefix: ast::Path::from_ident(
284-
Ident::from_str_and_span(
285-
"super", item_span,
286-
)
287-
),
288-
kind: ast::UseTreeKind::Glob,
289-
span: item_span,
290-
}),
291-
tokens: None,
292-
}),
293-
Box::new(ast::Item {
294-
attrs,
295-
id: DUMMY_NODE_ID,
296-
span: item_span,
297-
vis: ast::Visibility {
298-
span: eii_attr_span,
299-
kind: ast::VisibilityKind::Inherited,
300-
tokens: None
301-
},
302-
kind: ItemKind::Fn(Box::new(default_func)),
303-
tokens: None,
304-
}),
305-
],
306-
ast::Inline::Yes,
307-
ast::ModSpans {
308-
inner_span: item_span,
309-
inject_use_span: item_span,
310-
}
311-
)
312-
),
313-
tokens: None,
314-
})),
315-
span: eii_attr_span,
316-
}],
317-
id: DUMMY_NODE_ID,
318-
rules: ast::BlockCheckMode::Default,
319-
span: eii_attr_span,
320-
tokens: None,
321-
}),
322-
None,
223+
let item_mod = |span: Span, name: Ident, items: ThinVec<Box<ast::Item>>| {
224+
ecx.item(
225+
item_span,
226+
ThinVec::new(),
227+
ItemKind::Mod(
228+
ast::Safety::Default,
229+
name,
230+
ast::ModKind::Loaded(
231+
items,
232+
ast::Inline::Yes,
233+
ast::ModSpans { inner_span: span, inject_use_span: span },
323234
),
324-
span: eii_attr_span,
325-
attrs: ThinVec::new(),
326-
tokens: None,
327-
}))),
328-
define_opaque: None,
329-
})),
330-
tokens: None,
331-
}
235+
),
236+
)
237+
};
238+
239+
let anon_mod = |span: Span, stmts: ThinVec<ast::Stmt>| {
240+
let unit = ecx.ty(item_span, ast::TyKind::Tup(ThinVec::new()));
241+
let underscore = Ident::new(kw::Underscore, item_span);
242+
ecx.item_const(
243+
span,
244+
underscore,
245+
unit,
246+
ast::ConstItemRhs::Body(ecx.expr_block(ecx.block(span, stmts))),
247+
)
248+
};
249+
250+
// const _: () = {
251+
// mod dflt {
252+
// use super::*;
253+
// <orig fn>
254+
// }
255+
// }
256+
anon_mod(
257+
item_span,
258+
thin_vec![ecx.stmt_item(
259+
item_span,
260+
item_mod(
261+
item_span,
262+
Ident::from_str_and_span("dflt", item_span),
263+
thin_vec![
264+
ecx.item(
265+
item_span,
266+
thin_vec![ecx.attr_nested_word(sym::allow, sym::unused_imports, item_span)],
267+
ItemKind::Use(ast::UseTree {
268+
prefix: ast::Path::from_ident(Ident::from_str_and_span(
269+
"super", item_span,
270+
)),
271+
kind: ast::UseTreeKind::Glob,
272+
span: item_span,
273+
})
274+
),
275+
ecx.item(item_span, attrs, ItemKind::Fn(Box::new(default_func)))
276+
]
277+
)
278+
),],
279+
)
332280
}
333281

334282
/// Generates a foreign item, like
@@ -343,7 +291,7 @@ fn generate_foreign_item(
343291
mut func: ast::Fn,
344292
vis: Visibility,
345293
attrs_from_decl: &[Attribute],
346-
) -> ast::Item {
294+
) -> Box<ast::Item> {
347295
let mut foreign_item_attrs = ThinVec::new();
348296
foreign_item_attrs.extend_from_slice(attrs_from_decl);
349297

@@ -375,16 +323,10 @@ fn generate_foreign_item(
375323
func.sig.header.safety = ast::Safety::Safe(func.sig.span);
376324
}
377325

378-
ast::Item {
379-
attrs: ast::AttrVec::default(),
380-
id: ast::DUMMY_NODE_ID,
381-
span: eii_attr_span,
382-
vis: ast::Visibility {
383-
span: eii_attr_span,
384-
kind: ast::VisibilityKind::Inherited,
385-
tokens: None,
386-
},
387-
kind: ast::ItemKind::ForeignMod(ast::ForeignMod {
326+
ecx.item(
327+
eii_attr_span,
328+
ThinVec::new(),
329+
ast::ItemKind::ForeignMod(ast::ForeignMod {
388330
extern_span: eii_attr_span,
389331
safety: ast::Safety::Unsafe(eii_attr_span),
390332
abi,
@@ -397,8 +339,7 @@ fn generate_foreign_item(
397339
tokens: None,
398340
})]),
399341
}),
400-
tokens: None,
401-
}
342+
)
402343
}
403344

404345
/// Generate a stub macro (a bit like in core) that will roughly look like:
@@ -418,7 +359,7 @@ fn generate_attribute_macro_to_implement(
418359
foreign_item_name: Ident,
419360
impl_unsafe: bool,
420361
attrs_from_decl: &[Attribute],
421-
) -> ast::Item {
362+
) -> Box<ast::Item> {
422363
let mut macro_attrs = ThinVec::new();
423364

424365
// To avoid e.g. `error: attribute macro has missing stability attribute`
@@ -428,7 +369,8 @@ fn generate_attribute_macro_to_implement(
428369
// #[builtin_macro(eii_shared_macro)]
429370
macro_attrs.push(ecx.attr_nested_word(sym::rustc_builtin_macro, sym::eii_shared_macro, span));
430371

431-
ast::Item {
372+
// cant use ecx methods here to construct item since we need it to be public
373+
Box::new(ast::Item {
432374
attrs: macro_attrs,
433375
id: ast::DUMMY_NODE_ID,
434376
span,
@@ -467,7 +409,7 @@ fn generate_attribute_macro_to_implement(
467409
},
468410
),
469411
tokens: None,
470-
}
412+
})
471413
}
472414

473415
pub(crate) fn eii_extern_target(

0 commit comments

Comments
 (0)