Skip to content

Commit 108b3f2

Browse files
Properly gate safe keyword in pre-expansion
1 parent 1aaab8b commit 108b3f2

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

+4
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,10 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
562562
gate_all!(precise_capturing, "precise captures on `impl Trait` are experimental");
563563
gate_all!(global_registration, "global registration is experimental");
564564
gate_all!(unsafe_attributes, "`#[unsafe()]` markers for attributes are experimental");
565+
gate_all!(
566+
unsafe_extern_blocks,
567+
"`unsafe extern {}` blocks and `safe` keyword are experimental"
568+
);
565569

566570
if !visitor.features.never_patterns {
567571
if let Some(spans) = spans.get(&sym::never_patterns) {

compiler/rustc_parse/src/parser/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,9 @@ impl<'a> Parser<'a> {
12141214
if self.eat_keyword_case(kw::Unsafe, case) {
12151215
Safety::Unsafe(self.prev_token.uninterpolated_span())
12161216
} else if self.eat_keyword_case(kw::Safe, case) {
1217+
self.psess
1218+
.gated_spans
1219+
.gate(sym::unsafe_extern_blocks, self.prev_token.uninterpolated_span());
12171220
Safety::Safe(self.prev_token.uninterpolated_span())
12181221
} else {
12191222
Safety::Default

tests/ui/feature-gates/feature-gate-unsafe-extern-blocks.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,12 @@ unsafe extern "C" {
22
//~^ ERROR extern block cannot be declared unsafe
33
}
44

5+
// We can't gate `unsafe extern` blocks themselves since they were previously
6+
// allowed, but we should gate the `safe` soft keyword.
7+
#[cfg(any())]
8+
unsafe extern "C" {
9+
safe fn foo();
10+
//~^ ERROR `unsafe extern {}` blocks and `safe` keyword are experimental
11+
}
12+
513
fn main() {}

tests/ui/feature-gates/feature-gate-unsafe-extern-blocks.stderr

+12-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,16 @@ error: extern block cannot be declared unsafe
44
LL | unsafe extern "C" {
55
| ^^^^^^
66

7-
error: aborting due to 1 previous error
7+
error[E0658]: `unsafe extern {}` blocks and `safe` keyword are experimental
8+
--> $DIR/feature-gate-unsafe-extern-blocks.rs:9:5
9+
|
10+
LL | safe fn foo();
11+
| ^^^^
12+
|
13+
= note: see issue #123743 <https://github.com/rust-lang/rust/issues/123743> for more information
14+
= help: add `#![feature(unsafe_extern_blocks)]` to the crate attributes to enable
15+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
16+
17+
error: aborting due to 2 previous errors
818

19+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)