Skip to content

Commit 42a91fd

Browse files
committed
Gate and validate #[rustc_safe_intrinsic]
1 parent aadb571 commit 42a91fd

File tree

6 files changed

+69
-1
lines changed

6 files changed

+69
-1
lines changed

Diff for: compiler/rustc_feature/src/builtin_attrs.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,6 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
537537
allow_internal_unsafe, Normal, template!(Word), WarnFollowing,
538538
"allow_internal_unsafe side-steps the unsafe_code lint",
539539
),
540-
ungated!(rustc_safe_intrinsic, Normal, template!(Word), DuplicatesOk),
541540
rustc_attr!(rustc_allowed_through_unstable_modules, Normal, template!(Word), WarnFollowing,
542541
"rustc_allowed_through_unstable_modules special cases accidental stabilizations of stable items \
543542
through unstable paths"),
@@ -806,6 +805,10 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
806805
rustc_doc_primitive, Normal, template!(NameValueStr: "primitive name"), ErrorFollowing,
807806
r#"`rustc_doc_primitive` is a rustc internal attribute"#,
808807
),
808+
rustc_attr!(
809+
rustc_safe_intrinsic, Normal, template!(Word), WarnFollowing,
810+
"the `#[rustc_safe_intrinsic]` attribute is used internally to mark intrinsics as safe"
811+
),
809812

810813
// ==========================================================================
811814
// Internal attributes, Testing:

Diff for: compiler/rustc_passes/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,10 @@ passes_rustc_lint_opt_ty =
648648
`#[rustc_lint_opt_ty]` should be applied to a struct
649649
.label = not a struct
650650
651+
passes_rustc_safe_intrinsic =
652+
attribute should be applied to intrinsic functions
653+
.label = not an intrinsic function
654+
651655
passes_rustc_std_internal_symbol =
652656
attribute should be applied to functions or statics
653657
.label = not a function or static

Diff for: compiler/rustc_passes/src/check_attr.rs

+26
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ impl CheckAttrVisitor<'_> {
195195
| sym::rustc_promotable => self.check_stability_promotable(&attr, span, target),
196196
sym::link_ordinal => self.check_link_ordinal(&attr, span, target),
197197
sym::rustc_confusables => self.check_confusables(&attr, target),
198+
sym::rustc_safe_intrinsic => {
199+
self.check_rustc_safe_intrinsic(hir_id, attr, span, target)
200+
}
198201
_ => true,
199202
};
200203

@@ -2042,6 +2045,29 @@ impl CheckAttrVisitor<'_> {
20422045
}
20432046
}
20442047

2048+
fn check_rustc_safe_intrinsic(
2049+
&self,
2050+
hir_id: HirId,
2051+
attr: &Attribute,
2052+
span: Span,
2053+
target: Target,
2054+
) -> bool {
2055+
let hir = self.tcx.hir();
2056+
2057+
if let Target::ForeignFn = target
2058+
&& let Some(parent) = hir.opt_parent_id(hir_id)
2059+
&& let hir::Node::Item(Item {
2060+
kind: ItemKind::ForeignMod { abi: Abi::RustIntrinsic | Abi::PlatformIntrinsic, .. },
2061+
..
2062+
}) = hir.get(parent)
2063+
{
2064+
return true;
2065+
}
2066+
2067+
self.tcx.sess.emit_err(errors::RustcSafeIntrinsic { attr_span: attr.span, span });
2068+
false
2069+
}
2070+
20452071
fn check_rustc_std_internal_symbol(
20462072
&self,
20472073
attr: &Attribute,

Diff for: compiler/rustc_passes/src/errors.rs

+9
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,15 @@ pub struct RustcAllowConstFnUnstable {
620620
pub span: Span,
621621
}
622622

623+
#[derive(Diagnostic)]
624+
#[diag(passes_rustc_safe_intrinsic)]
625+
pub struct RustcSafeIntrinsic {
626+
#[primary_span]
627+
pub attr_span: Span,
628+
#[label]
629+
pub span: Span,
630+
}
631+
623632
#[derive(Diagnostic)]
624633
#[diag(passes_rustc_std_internal_symbol)]
625634
pub struct RustcStdInternalSymbol {

Diff for: tests/ui/intrinsics/feature-gate-safe-intrinsic.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#[rustc_safe_intrinsic]
2+
//~^ ERROR the `#[rustc_safe_intrinsic]` attribute is used internally to mark intrinsics as safe
3+
//~| ERROR attribute should be applied to intrinsic functions
4+
fn safe() {}
5+
6+
fn main() {}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0658]: the `#[rustc_safe_intrinsic]` attribute is used internally to mark intrinsics as safe
2+
--> $DIR/feature-gate-safe-intrinsic.rs:1:1
3+
|
4+
LL | #[rustc_safe_intrinsic]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: add `#![feature(rustc_attrs)]` to the crate attributes to enable
8+
9+
error: attribute should be applied to intrinsic functions
10+
--> $DIR/feature-gate-safe-intrinsic.rs:1:1
11+
|
12+
LL | #[rustc_safe_intrinsic]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^
14+
...
15+
LL | fn safe() {}
16+
| ------------ not an intrinsic function
17+
18+
error: aborting due to 2 previous errors
19+
20+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)