Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly gate safe keyword in pre-expansion #126757

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,10 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
gate_all!(precise_capturing, "precise captures on `impl Trait` are experimental");
gate_all!(global_registration, "global registration is experimental");
gate_all!(unsafe_attributes, "`#[unsafe()]` markers for attributes are experimental");
gate_all!(
unsafe_extern_blocks,
"`unsafe extern {}` blocks and `safe` keyword are experimental"
);

if !visitor.features.never_patterns {
if let Some(spans) = spans.get(&sym::never_patterns) {
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,9 @@ impl<'a> Parser<'a> {
if self.eat_keyword_case(kw::Unsafe, case) {
Safety::Unsafe(self.prev_token.uninterpolated_span())
} else if self.eat_keyword_case(kw::Safe, case) {
self.psess
.gated_spans
.gate(sym::unsafe_extern_blocks, self.prev_token.uninterpolated_span());
Safety::Safe(self.prev_token.uninterpolated_span())
} else {
Safety::Default
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/feature-gates/feature-gate-unsafe-extern-blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ unsafe extern "C" {
//~^ ERROR extern block cannot be declared unsafe
}

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

fn main() {}
13 changes: 12 additions & 1 deletion tests/ui/feature-gates/feature-gate-unsafe-extern-blocks.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,16 @@ error: extern block cannot be declared unsafe
LL | unsafe extern "C" {
| ^^^^^^

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

error: aborting due to 2 previous errors

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