Skip to content

Commit 9f32ccf

Browse files
committed
Auto merge of #146862 - matthiaskrgr:rollup-1zqootr, r=matthiaskrgr
Rollup of 4 pull requests Successful merges: - #143857 (Port #[macro_export] to the new attribute parsing infrastructure) - #146486 (Improve `core::sync::atomic` coverage) - #146606 (ci: x86_64-gnu-tools: Add `--test-args` regression test) - #146639 (std: merge definitions of `StdioPipes`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 7e4b8d7 + adfc111 commit 9f32ccf

40 files changed

+862
-408
lines changed

compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rustc_ast::AttrStyle;
12
use rustc_errors::DiagArgValue;
23
use rustc_hir::attrs::MacroUseArgs;
34

@@ -133,3 +134,65 @@ impl<S: Stage> NoArgsAttributeParser<S> for AllowInternalUnsafeParser {
133134
]);
134135
const CREATE: fn(Span) -> AttributeKind = |span| AttributeKind::AllowInternalUnsafe(span);
135136
}
137+
138+
pub(crate) struct MacroExportParser;
139+
140+
impl<S: Stage> SingleAttributeParser<S> for MacroExportParser {
141+
const PATH: &[Symbol] = &[sym::macro_export];
142+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
143+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
144+
const TEMPLATE: AttributeTemplate = template!(Word, List: &["local_inner_macros"]);
145+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowListWarnRest(&[
146+
Allow(Target::MacroDef),
147+
Error(Target::WherePredicate),
148+
Error(Target::Crate),
149+
]);
150+
151+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
152+
let suggestions = || {
153+
<Self as SingleAttributeParser<S>>::TEMPLATE
154+
.suggestions(AttrStyle::Inner, "macro_export")
155+
};
156+
let local_inner_macros = match args {
157+
ArgParser::NoArgs => false,
158+
ArgParser::List(list) => {
159+
let Some(l) = list.single() else {
160+
let span = cx.attr_span;
161+
cx.emit_lint(
162+
AttributeLintKind::InvalidMacroExportArguments {
163+
suggestions: suggestions(),
164+
},
165+
span,
166+
);
167+
return None;
168+
};
169+
match l.meta_item().and_then(|i| i.path().word_sym()) {
170+
Some(sym::local_inner_macros) => true,
171+
_ => {
172+
let span = cx.attr_span;
173+
cx.emit_lint(
174+
AttributeLintKind::InvalidMacroExportArguments {
175+
suggestions: suggestions(),
176+
},
177+
span,
178+
);
179+
return None;
180+
}
181+
}
182+
}
183+
ArgParser::NameValue(_) => {
184+
let span = cx.attr_span;
185+
let suggestions = suggestions();
186+
cx.emit_err(IllFormedAttributeInputLint {
187+
num_suggestions: suggestions.len(),
188+
suggestions: DiagArgValue::StrListSepByAnd(
189+
suggestions.into_iter().map(|s| format!("`{s}`").into()).collect(),
190+
),
191+
span,
192+
});
193+
return None;
194+
}
195+
};
196+
Some(AttributeKind::MacroExport { span: cx.attr_span, local_inner_macros })
197+
}
198+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use crate::attributes::lint_helpers::{
4040
};
4141
use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
4242
use crate::attributes::macro_attrs::{
43-
AllowInternalUnsafeParser, MacroEscapeParser, MacroUseParser,
43+
AllowInternalUnsafeParser, MacroEscapeParser, MacroExportParser, MacroUseParser,
4444
};
4545
use crate::attributes::must_use::MustUseParser;
4646
use crate::attributes::no_implicit_prelude::NoImplicitPreludeParser;
@@ -183,6 +183,7 @@ attribute_parsers!(
183183
Single<LinkOrdinalParser>,
184184
Single<LinkSectionParser>,
185185
Single<LinkageParser>,
186+
Single<MacroExportParser>,
186187
Single<MoveSizeLimitParser>,
187188
Single<MustUseParser>,
188189
Single<ObjcClassParser>,

compiler/rustc_attr_parsing/src/lints.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ pub fn emit_attribute_lint<L: LintEmitter>(lint: &AttributeLint<L::Id>, lint_emi
3131
},
3232
);
3333
}
34+
AttributeLintKind::InvalidMacroExportArguments { suggestions } => lint_emitter
35+
.emit_node_span_lint(
36+
rustc_session::lint::builtin::INVALID_MACRO_EXPORT_ARGUMENTS,
37+
*id,
38+
*span,
39+
session_diagnostics::IllFormedAttributeInput {
40+
num_suggestions: suggestions.len(),
41+
suggestions: DiagArgValue::StrListSepByAnd(
42+
suggestions.into_iter().map(|s| format!("`{s}`").into()).collect(),
43+
),
44+
},
45+
),
3446
AttributeLintKind::EmptyAttribute { first_span } => lint_emitter.emit_node_span_lint(
3547
rustc_session::lint::builtin::UNUSED_ATTRIBUTES,
3648
*id,

compiler/rustc_expand/src/base.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -942,9 +942,9 @@ impl SyntaxExtension {
942942
.unwrap_or_default();
943943
let allow_internal_unsafe = find_attr!(attrs, AttributeKind::AllowInternalUnsafe(_));
944944

945-
let local_inner_macros = ast::attr::find_by_name(attrs, sym::macro_export)
946-
.and_then(|macro_export| macro_export.meta_item_list())
947-
.is_some_and(|l| ast::attr::list_contains_name(&l, sym::local_inner_macros));
945+
let local_inner_macros =
946+
*find_attr!(attrs, AttributeKind::MacroExport {local_inner_macros: l, ..} => l)
947+
.unwrap_or(&false);
948948
let collapse_debuginfo = Self::get_collapse_debuginfo(sess, attrs, !is_local);
949949
tracing::debug!(?name, ?local_inner_macros, ?collapse_debuginfo, ?allow_internal_unsafe);
950950

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,9 @@ pub enum AttributeKind {
551551
/// Represents `#[macro_escape]`.
552552
MacroEscape(Span),
553553

554+
/// Represents [`#[macro_export]`](https://doc.rust-lang.org/reference/macros-by-example.html#r-macro.decl.scope.path).
555+
MacroExport { span: Span, local_inner_macros: bool },
556+
554557
/// Represents `#[rustc_macro_transparency]`.
555558
MacroTransparency(Transparency),
556559

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ impl AttributeKind {
5656
Linkage(..) => No,
5757
LoopMatch(..) => No,
5858
MacroEscape(..) => No,
59+
MacroExport { .. } => Yes,
5960
MacroTransparency(..) => Yes,
6061
MacroUse { .. } => No,
6162
Marker(..) => No,

compiler/rustc_hir/src/lints.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,34 @@ pub struct AttributeLint<Id> {
3131

3232
#[derive(Clone, Debug, HashStable_Generic)]
3333
pub enum AttributeLintKind {
34-
UnusedDuplicate { this: Span, other: Span, warning: bool },
35-
IllFormedAttributeInput { suggestions: Vec<String> },
36-
EmptyAttribute { first_span: Span },
37-
InvalidTarget { name: AttrPath, target: Target, applied: Vec<String>, only: &'static str },
38-
InvalidStyle { name: AttrPath, is_used_as_inner: bool, target: Target, target_span: Span },
34+
UnusedDuplicate {
35+
this: Span,
36+
other: Span,
37+
warning: bool,
38+
},
39+
IllFormedAttributeInput {
40+
suggestions: Vec<String>,
41+
},
42+
EmptyAttribute {
43+
first_span: Span,
44+
},
45+
46+
/// Copy of `IllFormedAttributeInput`
47+
/// specifically for the `invalid_macro_export_arguments` lint until that is removed,
48+
/// see <https://github.com/rust-lang/rust/pull/143857#issuecomment-3079175663>
49+
InvalidMacroExportArguments {
50+
suggestions: Vec<String>,
51+
},
52+
InvalidTarget {
53+
name: AttrPath,
54+
target: Target,
55+
applied: Vec<String>,
56+
only: &'static str,
57+
},
58+
InvalidStyle {
59+
name: AttrPath,
60+
is_used_as_inner: bool,
61+
target: Target,
62+
target_span: Span,
63+
},
3964
}

compiler/rustc_lint/src/non_local_def.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use rustc_errors::MultiSpan;
2+
use rustc_hir::attrs::AttributeKind;
23
use rustc_hir::def::{DefKind, Res};
34
use rustc_hir::intravisit::{self, Visitor, VisitorExt};
4-
use rustc_hir::{Body, HirId, Item, ItemKind, Node, Path, TyKind};
5+
use rustc_hir::{Body, HirId, Item, ItemKind, Node, Path, TyKind, find_attr};
56
use rustc_middle::ty::TyCtxt;
67
use rustc_session::{declare_lint, impl_lint_pass};
78
use rustc_span::def_id::{DefId, LOCAL_CRATE};
8-
use rustc_span::{ExpnKind, Span, kw, sym};
9+
use rustc_span::{ExpnKind, Span, kw};
910

1011
use crate::lints::{NonLocalDefinitionsCargoUpdateNote, NonLocalDefinitionsDiag};
1112
use crate::{LateContext, LateLintPass, LintContext, fluent_generated as fluent};
@@ -241,7 +242,10 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
241242
)
242243
}
243244
ItemKind::Macro(_, _macro, _kinds)
244-
if cx.tcx.has_attr(item.owner_id.def_id, sym::macro_export) =>
245+
if find_attr!(
246+
cx.tcx.get_all_attrs(item.owner_id.def_id),
247+
AttributeKind::MacroExport { .. }
248+
) =>
245249
{
246250
cx.emit_span_lint(
247251
NON_LOCAL_DEFINITIONS,

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4191,8 +4191,13 @@ declare_lint! {
41914191
/// You can't have multiple arguments in a `#[macro_export(..)]`, or mention arguments other than `local_inner_macros`.
41924192
///
41934193
pub INVALID_MACRO_EXPORT_ARGUMENTS,
4194-
Warn,
4194+
Deny,
41954195
"\"invalid_parameter\" isn't a valid argument for `#[macro_export]`",
4196+
@future_incompatible = FutureIncompatibleInfo {
4197+
reason: FutureIncompatibilityReason::FutureReleaseError,
4198+
reference: "issue #57571 <https://github.com/rust-lang/rust/issues/57571>",
4199+
report_in_deps: true,
4200+
};
41964201
}
41974202

41984203
declare_lint! {

compiler/rustc_passes/messages.ftl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,6 @@ passes_invalid_attr_at_crate_level =
349349
passes_invalid_attr_at_crate_level_item =
350350
the inner attribute doesn't annotate this {$kind}
351351
352-
passes_invalid_macro_export_arguments = invalid `#[macro_export]` argument
353-
354-
passes_invalid_macro_export_arguments_too_many_items = `#[macro_export]` can only take 1 or 0 arguments
355-
356352
passes_lang_item_fn = {$name ->
357353
[panic_impl] `#[panic_handler]`
358354
*[other] `{$name}` lang item
@@ -392,9 +388,6 @@ passes_loop_match_attr =
392388
`#[loop_match]` should be applied to a loop
393389
.label = not a loop
394390
395-
passes_macro_export =
396-
`#[macro_export]` only has an effect on macro definitions
397-
398391
passes_macro_export_on_decl_macro =
399392
`#[macro_export]` has no effect on declarative macro definitions
400393
.note = declarative macros follow the same exporting rules as regular items

0 commit comments

Comments
 (0)