-
Notifications
You must be signed in to change notification settings - Fork 12.1k
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
Unsafe extern blocks #124482
Open
spastorino
wants to merge
9
commits into
rust-lang:master
Choose a base branch
from
spastorino:unsafe-extern-blocks
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+589
−152
Open
Unsafe extern blocks #124482
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c1a067b
Add unsafe extern blocks failing tests
spastorino 2a03024
Allow unsafe extern on all editions
spastorino 28c2296
Test that unsafe extern defines unsafe fns
spastorino 522b7a2
Make extern blocks without unsafe warn in edition 2024
spastorino 5747a46
Allow using unsafe on functions inside extern blocks
spastorino ee041c0
Handle safety keyword for extern block inner items
spastorino 8d1dcf2
Fail when using safe/unsafe items inside unadorned extern blocks
spastorino 373d8ca
Add safe/unsafe to static inside extern blocks
spastorino 2b165b2
Add unsafe_extern_blocks feature flag
spastorino File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,8 @@ use rustc_data_structures::fx::FxIndexMap; | |
use rustc_feature::Features; | ||
use rustc_parse::validate_attr; | ||
use rustc_session::lint::builtin::{ | ||
DEPRECATED_WHERE_CLAUSE_LOCATION, MISSING_ABI, PATTERNS_IN_FNS_WITHOUT_BODY, | ||
DEPRECATED_WHERE_CLAUSE_LOCATION, MISSING_ABI, MISSING_UNSAFE_ON_EXTERN, | ||
PATTERNS_IN_FNS_WITHOUT_BODY, | ||
}; | ||
use rustc_session::lint::{BuiltinLintDiag, LintBuffer}; | ||
use rustc_session::Session; | ||
|
@@ -86,6 +87,9 @@ struct AstValidator<'a> { | |
/// or `Foo::Bar<impl Trait>` | ||
is_impl_trait_banned: bool, | ||
|
||
/// Used to ban explicit safety on foreign items when the extern block is not marked as unsafe. | ||
extern_mod_safety: Option<Safety>, | ||
|
||
lint_buffer: &'a mut LintBuffer, | ||
} | ||
|
||
|
@@ -116,6 +120,12 @@ impl<'a> AstValidator<'a> { | |
self.outer_trait_or_trait_impl = old; | ||
} | ||
|
||
fn with_in_extern_mod(&mut self, extern_mod_safety: Safety, f: impl FnOnce(&mut Self)) { | ||
let old = mem::replace(&mut self.extern_mod_safety, Some(extern_mod_safety)); | ||
f(self); | ||
self.extern_mod_safety = old; | ||
} | ||
|
||
fn with_banned_impl_trait(&mut self, f: impl FnOnce(&mut Self)) { | ||
let old = mem::replace(&mut self.is_impl_trait_banned, true); | ||
f(self); | ||
|
@@ -429,6 +439,19 @@ impl<'a> AstValidator<'a> { | |
} | ||
} | ||
|
||
fn check_foreign_item_safety(&self, item_span: Span, safety: Safety) { | ||
if matches!(safety, Safety::Unsafe(_) | Safety::Safe(_)) | ||
&& (self.features.unsafe_extern_blocks | ||
&& self.extern_mod_safety == Some(Safety::Default) | ||
|| !self.features.unsafe_extern_blocks) | ||
{ | ||
self.dcx().emit_err(errors::InvalidSafetyOnExtern { | ||
item_span, | ||
block: self.current_extern_span(), | ||
}); | ||
} | ||
} | ||
|
||
fn check_defaultness(&self, span: Span, defaultness: Defaultness) { | ||
if let Defaultness::Default(def_span) = defaultness { | ||
let span = self.session.source_map().guess_head_span(span); | ||
|
@@ -518,18 +541,14 @@ impl<'a> AstValidator<'a> { | |
fn check_foreign_fn_headerless( | ||
&self, | ||
// Deconstruct to ensure exhaustiveness | ||
FnHeader { safety, coroutine_kind, constness, ext }: FnHeader, | ||
FnHeader { safety: _, coroutine_kind, constness, ext }: FnHeader, | ||
) { | ||
let report_err = |span| { | ||
self.dcx().emit_err(errors::FnQualifierInExtern { | ||
span: span, | ||
block: self.current_extern_span(), | ||
}); | ||
}; | ||
match safety { | ||
Safety::Unsafe(span) => report_err(span), | ||
Safety::Default => (), | ||
} | ||
match coroutine_kind { | ||
Some(knd) => report_err(knd.span()), | ||
None => (), | ||
|
@@ -1017,19 +1036,34 @@ impl<'a> Visitor<'a> for AstValidator<'a> { | |
return; // Avoid visiting again. | ||
} | ||
ItemKind::ForeignMod(ForeignMod { abi, safety, .. }) => { | ||
let old_item = mem::replace(&mut self.extern_mod, Some(item)); | ||
self.visibility_not_permitted( | ||
&item.vis, | ||
errors::VisibilityNotPermittedNote::IndividualForeignItems, | ||
); | ||
if let &Safety::Unsafe(span) = safety { | ||
self.dcx().emit_err(errors::UnsafeItem { span, kind: "extern block" }); | ||
} | ||
if abi.is_none() { | ||
self.maybe_lint_missing_abi(item.span, item.id); | ||
} | ||
visit::walk_item(self, item); | ||
self.extern_mod = old_item; | ||
self.with_in_extern_mod(*safety, |this| { | ||
let old_item = mem::replace(&mut this.extern_mod, Some(item)); | ||
this.visibility_not_permitted( | ||
&item.vis, | ||
errors::VisibilityNotPermittedNote::IndividualForeignItems, | ||
); | ||
|
||
if this.features.unsafe_extern_blocks { | ||
if &Safety::Default == safety { | ||
this.lint_buffer.buffer_lint( | ||
MISSING_UNSAFE_ON_EXTERN, | ||
item.id, | ||
item.span, | ||
BuiltinLintDiag::MissingUnsafeOnExtern, | ||
); | ||
} | ||
Comment on lines
+1047
to
+1054
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Either here or in follow-up work, we need to make this into a hard error (rather than just (@oli-obk, what's your preference on whether this is done in this PR or later?) |
||
} else { | ||
if let &Safety::Unsafe(span) = safety { | ||
this.dcx().emit_err(errors::UnsafeItem { span, kind: "extern block" }); | ||
} | ||
} | ||
|
||
if abi.is_none() { | ||
this.maybe_lint_missing_abi(item.span, item.id); | ||
} | ||
visit::walk_item(this, item); | ||
this.extern_mod = old_item; | ||
}); | ||
return; // Avoid visiting again. | ||
} | ||
ItemKind::Enum(def, _) => { | ||
|
@@ -1161,6 +1195,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { | |
fn visit_foreign_item(&mut self, fi: &'a ForeignItem) { | ||
match &fi.kind { | ||
ForeignItemKind::Fn(box Fn { defaultness, sig, body, .. }) => { | ||
self.check_foreign_item_safety(fi.span, sig.header.safety); | ||
self.check_defaultness(fi.span, *defaultness); | ||
self.check_foreign_fn_bodyless(fi.ident, body.as_deref()); | ||
self.check_foreign_fn_headerless(sig.header); | ||
|
@@ -1180,7 +1215,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> { | |
self.check_foreign_ty_genericless(generics, where_clauses); | ||
self.check_foreign_item_ascii_only(fi.ident); | ||
} | ||
ForeignItemKind::Static(box StaticForeignItem { ty: _, mutability: _, expr }) => { | ||
ForeignItemKind::Static(box StaticForeignItem { expr, safety, .. }) => { | ||
self.check_foreign_item_safety(fi.span, *safety); | ||
self.check_foreign_kind_bodyless(fi.ident, "static", expr.as_ref().map(|b| b.span)); | ||
self.check_foreign_item_ascii_only(fi.ident); | ||
} | ||
|
@@ -1736,6 +1772,7 @@ pub fn check_crate( | |
outer_impl_trait: None, | ||
disallow_tilde_const: Some(DisallowTildeConstContext::Item), | ||
is_impl_trait_banned: false, | ||
extern_mod_safety: None, | ||
lint_buffer: lints, | ||
}; | ||
visit::walk_crate(&mut validator, krate); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is unfortunate but making
StaticItem
andStaticForeignItem
carry different types would require a major refactor mainly due to https://github.com/spastorino/rust/blob/f6f8e91adcaf446eb4b38fe960a97d0c1ab88417/compiler/rustc_parse/src/parser/item.rs#L1184 which is an ungreat way of code reusing.I can refactor this code to disentangle it after this PR lands and then remove this safety field and likely the expr one but I wouldn't block landing this PR on this pre-existing issue.