Skip to content

Commit 99a9fe1

Browse files
committed
Port #[ffi_const] to the new attribute system
1 parent bb8b741 commit 99a9fe1

File tree

8 files changed

+32
-10
lines changed

8 files changed

+32
-10
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ pub enum AttributeKind {
251251
/// Represents `#[export_stable]`.
252252
ExportStable,
253253

254+
/// Represents `#[ffi_const]`.
255+
FfiConst(Span),
256+
254257
/// Represents `#[ignore]`
255258
Ignore {
256259
span: Span,

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ impl AttributeKind {
2727
DocComment { .. } => Yes,
2828
ExportName { .. } => Yes,
2929
ExportStable => No,
30+
FfiConst(..) => No,
3031
Ignore { .. } => No,
3132
Inline(..) => No,
3233
LinkName { .. } => Yes,

compiler/rustc_attr_parsing/src/attributes/link_attrs.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,10 @@ impl<S: Stage> NoArgsAttributeParser<S> for ExportStableParser {
6666
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
6767
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::ExportStable;
6868
}
69+
70+
pub(crate) struct FfiConstParser;
71+
impl<S: Stage> NoArgsAttributeParser<S> for FfiConstParser {
72+
const PATH: &[Symbol] = &[sym::ffi_const];
73+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
74+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::FfiConst;
75+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ 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::{ExportStableParser, LinkNameParser, LinkSectionParser};
25+
use crate::attributes::link_attrs::{
26+
ExportStableParser, FfiConstParser, LinkNameParser, LinkSectionParser,
27+
};
2628
use crate::attributes::lint_helpers::{AsPtrParser, PassByValueParser, PubTransparentParser};
2729
use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
2830
use crate::attributes::must_use::MustUseParser;
@@ -146,6 +148,7 @@ attribute_parsers!(
146148
Single<WithoutArgs<ConstContinueParser>>,
147149
Single<WithoutArgs<ConstStabilityIndirectParser>>,
148150
Single<WithoutArgs<ExportStableParser>>,
151+
Single<WithoutArgs<FfiConstParser>>,
149152
Single<WithoutArgs<LoopMatchParser>>,
150153
Single<WithoutArgs<MayDangleParser>>,
151154
Single<WithoutArgs<NoImplicitPreludeParser>>,

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
203203
UsedBy::Compiler => codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED_COMPILER,
204204
UsedBy::Linker => codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED_LINKER,
205205
},
206+
AttributeKind::FfiConst(_) => {
207+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_CONST
208+
}
206209
_ => {}
207210
}
208211
}
@@ -214,7 +217,6 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
214217
match name {
215218
sym::rustc_allocator => codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR,
216219
sym::ffi_pure => codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_PURE,
217-
sym::ffi_const => codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_CONST,
218220
sym::rustc_nounwind => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND,
219221
sym::rustc_reallocator => codegen_fn_attrs.flags |= CodegenFnAttrFlags::REALLOCATOR,
220222
sym::rustc_deallocator => codegen_fn_attrs.flags |= CodegenFnAttrFlags::DEALLOCATOR,

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ pub fn check_builtin_meta_item(
272272
name,
273273
sym::inline
274274
| sym::export_stable
275+
| sym::ffi_const
275276
| sym::may_dangle
276277
| sym::rustc_as_ptr
277278
| sym::rustc_pub_transparent

compiler/rustc_passes/src/check_attr.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
207207
Attribute::Parsed(AttributeKind::ExportStable) => {
208208
// handled in `check_export`
209209
}
210+
&Attribute::Parsed(AttributeKind::FfiConst(attr_span)) => {
211+
self.check_ffi_const(attr_span, target)
212+
}
210213
Attribute::Parsed(
211214
AttributeKind::BodyStability { .. }
212215
| AttributeKind::ConstStabilityIndirect
@@ -304,7 +307,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
304307
self.check_has_incoherent_inherent_impls(attr, span, target)
305308
}
306309
[sym::ffi_pure, ..] => self.check_ffi_pure(attr.span(), attrs, target),
307-
[sym::ffi_const, ..] => self.check_ffi_const(attr.span(), target),
308310
[sym::link_ordinal, ..] => self.check_link_ordinal(attr, span, target),
309311
[sym::link, ..] => self.check_link(hir_id, attr, span, target),
310312
[sym::macro_use, ..] | [sym::macro_escape, ..] => {
@@ -1509,7 +1511,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
15091511
self.dcx().emit_err(errors::FfiPureInvalidTarget { attr_span });
15101512
return;
15111513
}
1512-
if attrs.iter().any(|a| a.has_name(sym::ffi_const)) {
1514+
if find_attr!(attrs, AttributeKind::FfiConst(_)) {
15131515
// `#[ffi_const]` functions cannot be `#[ffi_pure]`
15141516
self.dcx().emit_err(errors::BothFfiConstAndPure { attr_span });
15151517
}

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 `link_ordinal` attribute input
146146
LL | #[link_ordinal]
147147
| ^^^^^^^^^^^^^^^ help: must be of the form: `#[link_ordinal(ordinal)]`
148148

149-
error: malformed `ffi_const` attribute input
150-
--> $DIR/malformed-attrs.rs:171:5
151-
|
152-
LL | #[unsafe(ffi_const = 1)]
153-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[ffi_const]`
154-
155149
error: malformed `linkage` attribute input
156150
--> $DIR/malformed-attrs.rs:173:5
157151
|
@@ -540,6 +534,15 @@ LL | #[rustc_layout_scalar_valid_range_end]
540534
| expected this to be a list
541535
| help: must be of the form: `#[rustc_layout_scalar_valid_range_end(end)]`
542536

537+
error[E0565]: malformed `ffi_const` attribute input
538+
--> $DIR/malformed-attrs.rs:171:5
539+
|
540+
LL | #[unsafe(ffi_const = 1)]
541+
| ^^^^^^^^^^^^^^^^^^^---^^
542+
| | |
543+
| | didn't expect any arguments here
544+
| help: must be of the form: `#[ffi_const]`
545+
543546
error[E0565]: malformed `non_exhaustive` attribute input
544547
--> $DIR/malformed-attrs.rs:197:1
545548
|

0 commit comments

Comments
 (0)