Skip to content

Commit 6f4a0a1

Browse files
authored
Rollup merge of rust-lang#116162 - fmease:gate-n-validate-rustc_safe_intrinsic, r=Nilstrieb
Gate and validate `#[rustc_safe_intrinsic]` Copied over from rust-lang#116159: > This was added as ungated in https://github.com/rust-lang/rust/pull/100719/files#diff-09c366d3ad3ec9a42125253b610ca83cad6b156aa2a723f6c7e83eddef7b1e8fR502, probably because the author looked at the surrounding attributes, which are ungated because they are gated specially behind the staged_api feature. > > I don't think we need to crater this, the attribute is entirely useless without the intrinsics feature, which is already unstable.. r? ``@Nilstrieb``
2 parents 238a7b7 + f54db7c commit 6f4a0a1

File tree

14 files changed

+79
-11
lines changed

14 files changed

+79
-11
lines changed

compiler/rustc_error_codes/src/error_codes/E0094.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ An invalid number of generic parameters was passed to an intrinsic function.
33
Erroneous code example:
44

55
```compile_fail,E0094
6-
#![feature(intrinsics)]
6+
#![feature(intrinsics, rustc_attrs)]
77
#![allow(internal_features)]
88
99
extern "rust-intrinsic" {
@@ -18,7 +18,7 @@ and verify with the function declaration in the Rust source code.
1818
Example:
1919

2020
```
21-
#![feature(intrinsics)]
21+
#![feature(intrinsics, rustc_attrs)]
2222
#![allow(internal_features)]
2323
2424
extern "rust-intrinsic" {

compiler/rustc_error_codes/src/error_codes/E0211.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ You used a function or type which doesn't fit the requirements for where it was
44
used. Erroneous code examples:
55

66
```compile_fail
7-
#![feature(intrinsics)]
7+
#![feature(intrinsics, rustc_attrs)]
88
#![allow(internal_features)]
99
1010
extern "rust-intrinsic" {
@@ -41,7 +41,7 @@ impl Foo {
4141
For the first code example, please check the function definition. Example:
4242

4343
```
44-
#![feature(intrinsics)]
44+
#![feature(intrinsics, rustc_attrs)]
4545
#![allow(internal_features)]
4646
4747
extern "rust-intrinsic" {

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:

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

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,

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 {

tests/ui/error-codes/E0094.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(intrinsics)]
1+
#![feature(intrinsics, rustc_attrs)]
22

33
extern "rust-intrinsic" {
44
#[rustc_safe_intrinsic]

tests/ui/extern/extern-with-type-bounds.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(intrinsics)]
1+
#![feature(intrinsics, rustc_attrs)]
22

33
extern "rust-intrinsic" {
44
// Real example from libcore
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() {}
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`.

tests/ui/intrinsics/intrinsic-alignment.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// run-pass
22
// ignore-wasm32-bare seems not important to test here
33

4-
#![feature(intrinsics)]
4+
#![feature(intrinsics, rustc_attrs)]
55

66
mod rusti {
77
extern "rust-intrinsic" {

tests/ui/repr/16-bit-repr-c-enum.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// [avr] compile-flags: --target=avr-unknown-gnu-atmega328 --crate-type=rlib
66
// [msp430] needs-llvm-components: msp430
77
// [msp430] compile-flags: --target=msp430-none-elf --crate-type=rlib
8-
#![feature(no_core, lang_items, intrinsics, staged_api)]
8+
#![feature(no_core, lang_items, intrinsics, staged_api, rustc_attrs)]
99
#![no_core]
1010
#![crate_type = "lib"]
1111
#![stable(feature = "", since = "")]

tests/ui/structs-enums/rec-align-u32.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![allow(unused_unsafe)]
44
// Issue #2303
55

6-
#![feature(intrinsics)]
6+
#![feature(intrinsics, rustc_attrs)]
77

88
use std::mem;
99

tests/ui/structs-enums/rec-align-u64.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
// Issue #2303
77

8-
#![feature(intrinsics)]
8+
#![feature(intrinsics, rustc_attrs)]
99

1010
use std::mem;
1111

0 commit comments

Comments
 (0)