Skip to content

Commit 9fa6593

Browse files
committed
Port #[ffi_const] to the new attribute system
1 parent 4970d54 commit 9fa6593

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, PubTransparentParser};
2729
use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
2830
use crate::attributes::must_use::MustUseParser;
@@ -140,6 +142,7 @@ attribute_parsers!(
140142
Single<WithoutArgs<ConstContinueParser>>,
141143
Single<WithoutArgs<ConstStabilityIndirectParser>>,
142144
Single<WithoutArgs<ExportStableParser>>,
145+
Single<WithoutArgs<FfiConstParser>>,
143146
Single<WithoutArgs<LoopMatchParser>>,
144147
Single<WithoutArgs<MayDangleParser>>,
145148
Single<WithoutArgs<NoMangleParser>>,

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
@@ -193,6 +193,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
193193
Attribute::Parsed(AttributeKind::ExportStable) => {
194194
// handled in `check_export`
195195
}
196+
&Attribute::Parsed(AttributeKind::FfiConst(attr_span)) => {
197+
self.check_ffi_const(attr_span, target)
198+
}
196199
Attribute::Parsed(
197200
AttributeKind::BodyStability { .. }
198201
| AttributeKind::ConstStabilityIndirect
@@ -286,7 +289,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
286289
self.check_has_incoherent_inherent_impls(attr, span, target)
287290
}
288291
[sym::ffi_pure, ..] => self.check_ffi_pure(attr.span(), attrs, target),
289-
[sym::ffi_const, ..] => self.check_ffi_const(attr.span(), target),
290292
[sym::link_ordinal, ..] => self.check_link_ordinal(attr, span, target),
291293
[sym::link, ..] => self.check_link(hir_id, attr, span, target),
292294
[sym::macro_use, ..] | [sym::macro_escape, ..] => {
@@ -1474,7 +1476,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
14741476
self.dcx().emit_err(errors::FfiPureInvalidTarget { attr_span });
14751477
return;
14761478
}
1477-
if attrs.iter().any(|a| a.has_name(sym::ffi_const)) {
1479+
if find_attr!(attrs, AttributeKind::FfiConst(_)) {
14781480
// `#[ffi_const]` functions cannot be `#[ffi_pure]`
14791481
self.dcx().emit_err(errors::BothFfiConstAndPure { attr_span });
14801482
}

tests/ui/attributes/malformed-attrs.stderr

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,6 @@ error: malformed `link_ordinal` attribute input
152152
LL | #[link_ordinal]
153153
| ^^^^^^^^^^^^^^^ help: must be of the form: `#[link_ordinal(ordinal)]`
154154

155-
error: malformed `ffi_const` attribute input
156-
--> $DIR/malformed-attrs.rs:170:5
157-
|
158-
LL | #[unsafe(ffi_const = 1)]
159-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[ffi_const]`
160-
161155
error: malformed `linkage` attribute input
162156
--> $DIR/malformed-attrs.rs:172:5
163157
|
@@ -552,6 +546,15 @@ LL | #[rustc_layout_scalar_valid_range_end]
552546
| expected this to be a list
553547
| help: must be of the form: `#[rustc_layout_scalar_valid_range_end(end)]`
554548

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

0 commit comments

Comments
 (0)