Skip to content

Commit 1624463

Browse files
committed
Port #[link_ordinal] to the new attribute parsing infrastructure.
1 parent dee4ae2 commit 1624463

File tree

4 files changed

+65
-10
lines changed

4 files changed

+65
-10
lines changed

compiler/rustc_attr_parsing/src/attributes/link_attrs.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ use rustc_span::{Symbol, sym};
77
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser};
88
use crate::context::{AcceptContext, Stage};
99
use crate::parser::ArgParser;
10-
use crate::session_diagnostics::{InvalidLinkOrdinalFormat, LinkOrdinalOutOfRange};
11-
use crate::session_diagnostics::NullOnLinkSection;
10+
use crate::session_diagnostics::{
11+
InvalidLinkOrdinalFormat, LinkOrdinalOutOfRange, NullOnLinkSection,
12+
};
1213

1314
pub(crate) struct LinkNameParser;
1415

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use crate::attributes::codegen_attrs::{
2222
use crate::attributes::confusables::ConfusablesParser;
2323
use crate::attributes::deprecation::DeprecationParser;
2424
use crate::attributes::inline::{InlineParser, RustcForceInlineParser};
25-
use crate::attributes::link_attrs::{LinkNameParser, LinkSectionParser, LinkOrdinalParser};
26-
use crate::attributes::lint_helpers::{AsPtrParser, PubTransparentParser, PassByValueParser};
25+
use crate::attributes::link_attrs::{LinkNameParser, LinkOrdinalParser, LinkSectionParser};
26+
use crate::attributes::lint_helpers::{AsPtrParser, PassByValueParser, PubTransparentParser};
2727
use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
2828
use crate::attributes::must_use::MustUseParser;
2929
use crate::attributes::no_implicit_prelude::NoImplicitPreludeParser;

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,57 @@ pub fn check_builtin_meta_item(
267267
deny_unsafety: bool,
268268
) {
269269
if !is_attr_template_compatible(&template, &meta.kind) {
270+
emit_malformed_attribute(psess, style, meta.span, name, template);
271+
}
272+
273+
if deny_unsafety {
274+
deny_builtin_meta_unsafety(psess, meta);
275+
}
276+
}
277+
278+
fn emit_malformed_attribute(
279+
psess: &ParseSess,
280+
style: ast::AttrStyle,
281+
span: Span,
282+
name: Symbol,
283+
template: AttributeTemplate,
284+
) {
285+
// attrs with new parsers are locally validated so excluded here
286+
if matches!(
287+
name,
288+
sym::inline
289+
| sym::may_dangle
290+
| sym::rustc_as_ptr
291+
| sym::rustc_pub_transparent
292+
| sym::rustc_const_stable_indirect
293+
| sym::rustc_force_inline
294+
| sym::rustc_confusables
295+
| sym::rustc_skip_during_method_dispatch
296+
| sym::rustc_pass_by_value
297+
| sym::repr
298+
| sym::align
299+
| sym::deprecated
300+
| sym::optimize
301+
| sym::cold
302+
| sym::target_feature
303+
| sym::rustc_allow_const_fn_unstable
304+
| sym::naked
305+
| sym::no_mangle
306+
| sym::non_exhaustive
307+
| sym::path
308+
| sym::ignore
309+
| sym::must_use
310+
| sym::track_caller
311+
| sym::link_ordinal
312+
| sym::link_name
313+
| sym::export_name
314+
| sym::rustc_macro_transparency
315+
| sym::link_section
316+
| sym::rustc_layout_scalar_valid_range_start
317+
| sym::rustc_layout_scalar_valid_range_end
318+
| sym::no_implicit_prelude
319+
) {
320+
return;
270321
// attrs with new parsers are locally validated so excluded here
271322
if matches!(
272323
name,

tests/ui/attributes/malformed-attrs.stderr

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,6 @@ error: malformed `ffi_pure` attribute input
146146
LL | #[unsafe(ffi_pure = 1)]
147147
| ^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[ffi_pure]`
148148

149-
error: malformed `link_ordinal` attribute input
150-
--> $DIR/malformed-attrs.rs:167:5
151-
|
152-
LL | #[link_ordinal]
153-
| ^^^^^^^^^^^^^^^ help: must be of the form: `#[link_ordinal(ordinal)]`
154-
155149
error: malformed `ffi_const` attribute input
156150
--> $DIR/malformed-attrs.rs:171:5
157151
|
@@ -537,6 +531,15 @@ LL | #[rustc_layout_scalar_valid_range_end]
537531
| expected this to be a list
538532
| help: must be of the form: `#[rustc_layout_scalar_valid_range_end(end)]`
539533

534+
error[E0539]: malformed `link_ordinal` attribute input
535+
--> $DIR/malformed-attrs.rs:167:5
536+
|
537+
LL | #[link_ordinal]
538+
| ^^^^^^^^^^^^^^^
539+
| |
540+
| expected this to be a list
541+
| help: must be of the form: `#[link_ordinal(ordinal)]`
542+
540543
error[E0565]: malformed `non_exhaustive` attribute input
541544
--> $DIR/malformed-attrs.rs:197:1
542545
|

0 commit comments

Comments
 (0)