Skip to content

Commit 90bb6b1

Browse files
committed
Port #[ffi_const] to the new attribute system
1 parent c276ab2 commit 90bb6b1

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
@@ -253,6 +253,9 @@ pub enum AttributeKind {
253253
/// Represents `#[export_stable]`.
254254
ExportStable,
255255

256+
/// Represents `#[ffi_const]`.
257+
FfiConst(Span),
258+
256259
/// Represents `#[inline]` and `#[rustc_force_inline]`.
257260
Inline(InlineAttr, Span),
258261

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
Inline(..) => No,
3132
LinkName { .. } => Yes,
3233
LinkSection { .. } => No,

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;
@@ -141,6 +143,7 @@ attribute_parsers!(
141143
Single<WithoutArgs<ConstContinueParser>>,
142144
Single<WithoutArgs<ConstStabilityIndirectParser>>,
143145
Single<WithoutArgs<ExportStableParser>>,
146+
Single<WithoutArgs<FfiConstParser>>,
144147
Single<WithoutArgs<LoopMatchParser>>,
145148
Single<WithoutArgs<MayDangleParser>>,
146149
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
@@ -211,6 +211,9 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
211211
UsedBy::Compiler => codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED_COMPILER,
212212
UsedBy::Linker => codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED_LINKER,
213213
},
214+
AttributeKind::FfiConst(_) => {
215+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_CONST
216+
}
214217
_ => {}
215218
}
216219
}
@@ -222,7 +225,6 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
222225
match name {
223226
sym::rustc_allocator => codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR,
224227
sym::ffi_pure => codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_PURE,
225-
sym::ffi_const => codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_CONST,
226228
sym::rustc_nounwind => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND,
227229
sym::rustc_reallocator => codegen_fn_attrs.flags |= CodegenFnAttrFlags::REALLOCATOR,
228230
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
@@ -287,6 +287,7 @@ fn emit_malformed_attribute(
287287
name,
288288
sym::inline
289289
| sym::export_stable
290+
| sym::ffi_const
290291
| sym::may_dangle
291292
| sym::rustc_as_ptr
292293
| 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
@@ -201,6 +201,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
201201
Attribute::Parsed(AttributeKind::ExportStable) => {
202202
// handled in `check_export`
203203
}
204+
&Attribute::Parsed(AttributeKind::FfiConst(attr_span)) => {
205+
self.check_ffi_const(attr_span, target)
206+
}
204207
Attribute::Parsed(
205208
AttributeKind::BodyStability { .. }
206209
| AttributeKind::ConstStabilityIndirect
@@ -296,7 +299,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
296299
self.check_has_incoherent_inherent_impls(attr, span, target)
297300
}
298301
[sym::ffi_pure, ..] => self.check_ffi_pure(attr.span(), attrs, target),
299-
[sym::ffi_const, ..] => self.check_ffi_const(attr.span(), target),
300302
[sym::link_ordinal, ..] => self.check_link_ordinal(attr, span, target),
301303
[sym::link, ..] => self.check_link(hir_id, attr, span, target),
302304
[sym::macro_use, ..] | [sym::macro_escape, ..] => {
@@ -1503,7 +1505,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
15031505
self.dcx().emit_err(errors::FfiPureInvalidTarget { attr_span });
15041506
return;
15051507
}
1506-
if attrs.iter().any(|a| a.has_name(sym::ffi_const)) {
1508+
if find_attr!(attrs, AttributeKind::FfiConst(_)) {
15071509
// `#[ffi_const]` functions cannot be `#[ffi_pure]`
15081510
self.dcx().emit_err(errors::BothFfiConstAndPure { attr_span });
15091511
}

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:170: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:172:5
157151
|
@@ -555,6 +549,15 @@ LL | #[rustc_layout_scalar_valid_range_end]
555549
| expected this to be a list
556550
| help: must be of the form: `#[rustc_layout_scalar_valid_range_end(end)]`
557551

552+
error[E0565]: malformed `ffi_const` attribute input
553+
--> $DIR/malformed-attrs.rs:170:5
554+
|
555+
LL | #[unsafe(ffi_const = 1)]
556+
| ^^^^^^^^^^^^^^^^^^^---^^
557+
| | |
558+
| | didn't expect any arguments here
559+
| help: must be of the form: `#[ffi_const]`
560+
558561
error: attribute should be applied to `const fn`
559562
--> $DIR/malformed-attrs.rs:34:1
560563
|

0 commit comments

Comments
 (0)