-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Parse unsafe attributes #124214
Parse unsafe attributes #124214
Conversation
rustbot has assigned @michaelwoerister. Use |
This comment has been minimized.
This comment has been minimized.
☔ The latest upstream changes (presumably #124208) made this pull request unmergeable. Please resolve the merge conflicts. |
0702557
to
6ebb485
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
☔ The latest upstream changes (presumably #124360) made this pull request unmergeable. Please resolve the merge conflicts. |
Thanks for the PR, @carbotaniuman! Would you mind also adding a test case where using |
c91ab4f
to
eb16919
Compare
☔ The latest upstream changes (presumably #124424) made this pull request unmergeable. Please resolve the merge conflicts. |
eb16919
to
5562e99
Compare
I've added stuff to handle the |
compiler/rustc_ast/src/token.rs
Outdated
@@ -911,7 +912,7 @@ impl NonterminalKind { | |||
sym::ident => NonterminalKind::Ident, | |||
sym::lifetime => NonterminalKind::Lifetime, | |||
sym::literal => NonterminalKind::Literal, | |||
sym::meta => NonterminalKind::Meta, | |||
sym::meta => NonterminalKind::Meta2021, |
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.
Is this actually needed? The grammar doesn't change, right?
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.
I'm not sure...
#[cfg_attr(not(debug), unsafe(Foo))]
struct Foo;
The above is a parse error right now as it expects a path and not a keyword. There were 2 implementations strategies I considered, one of which was defining a macro unsafe
that created the Attribute
with the proper unsafety, but given that I would still need syntax changes (to deal with the unsafe
), I opted to instead change the core AttrItem
itself to have an unsafety
field. So I'm not really sure if a) this is the right way of doing things and b) if I need to make the macro changes.
ast::Unsafe::No | ||
}; | ||
|
||
let path = this.parse_path(PathStyle::Mod)?; |
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.
I expect this to result in a slightly weird error message when doing something like #[unsafe(unsafe(no_mangle))]
. Can we add a test case for that?
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.
error: expected identifier, found keyword `unsafe`
--> /home/quydx/code/rust/tests/ui/attributes/unsafe/double-unsafe-in-attribute.rs:1:10
|
LL | #[unsafe(unsafe(no_mangle))]
| ^^^^^^ expected identifier, found keyword
|
help: escape `unsafe` to use it as an identifier
|
LL | #[unsafe(r#unsafe(no_mangle))]
| ++
error: cannot find attribute `r#unsafe` in this scope
--> /home/quydx/code/rust/tests/ui/attributes/unsafe/double-unsafe-in-attribute.rs:1:10
|
LL | #[unsafe(unsafe(no_mangle))]
| ^^^^^^
The error is basically just what we have now. I can probably add a note saying that unsafe(...)
doesn't nest.
@@ -170,6 +175,7 @@ impl<'a> Parser<'a> { | |||
NtPath(P(self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?)) | |||
} | |||
NonterminalKind::Meta => NtMeta(P(self.parse_attr_item(true)?)), | |||
NonterminalKind::Meta2021 => NtMeta(P(self.parse_attr_item_no_unsafe(true)?)), |
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.
Again, I'm not sure this is necessary because using unsafe(...)
currently fails to parse (see playground example). So I think this should fall under the following paragraph from RFC 3531:
In cases where we’re adding new syntax and updating the grammar to include that new syntax, if we can update the corresponding fragment specifier simultaneously to match the new grammar in such a way that we do not risk changing the behavior of existing macros (i.e., because the new syntax previously would have failed parsing), then we will do that so as to prevent or minimize divergence between the fragment specifier and the new grammar.
I.e. the question is: Can the addition of unsafe(..)
change the behavior of any currently valid macros / macro invocations?
I think we need some expert input. There are two main questions:
@rustbot ping parser |
It looks like rustbot does not allow pinging adhoc groups, so pinging manually regarding the questions in the comment above: @compiler-errors @davidtwco @estebank @nnethercote @petrochenkov @spastorino |
This is the right strategy. |
It would be good if There may be some issues with ambiguities between macro arms in cases similar to macro_rules! m {
(unsafe) => { 1 }
($meta:meta) => { 2 }
}
m!(unsafe(no_mangle)); This needs some testing. |
Thanks for the feedback!
Yes, I read through the RFC discussion thread today. I'm going to stay neutral regarding the choice 🙂 |
9fdd56f
to
f83cdbd
Compare
This comment has been minimized.
This comment has been minimized.
@bors r+ |
…r=michaelwoerister Parse unsafe attributes Initial parse implementation for rust-lang#123757 This is the initial work to parse unsafe attributes, which is represented as an extra `unsafety` field in `MetaItem` and `AttrItem`. There's two areas in the code where it appears that parsing is done manually and not using the parser stuff, and I'm not sure how I'm supposed to thread the change there.
…iaskrgr Rollup of 11 pull requests Successful merges: - rust-lang#116088 (Stabilise `c_unwind`) - rust-lang#124012 (Stabilize `binary_heap_as_slice`) - rust-lang#124214 (Parse unsafe attributes) - rust-lang#125572 (Detect pub structs never constructed and unused associated constants) - rust-lang#125781 (prefer `compile::stream_cargo` for building tools) - rust-lang#126030 (Update `./x fmt` command in library/std/src/sys/pal/windows/c/README.md) - rust-lang#126047 (Simplify the rayon calls in the installer) - rust-lang#126070 (Enable f16 in assembly on aarch64 platforms that support it) - rust-lang#126089 (Stabilize Option::take_if) - rust-lang#126112 (Clean up source root in run-make tests) - rust-lang#126119 (Improve docs for using custom paths with `--emit`) r? `@ghost` `@rustbot` modify labels: rollup
…r=michaelwoerister Parse unsafe attributes Initial parse implementation for rust-lang#123757 This is the initial work to parse unsafe attributes, which is represented as an extra `unsafety` field in `MetaItem` and `AttrItem`. There's two areas in the code where it appears that parsing is done manually and not using the parser stuff, and I'm not sure how I'm supposed to thread the change there.
…iaskrgr Rollup of 10 pull requests Successful merges: - rust-lang#116088 (Stabilise `c_unwind`) - rust-lang#124012 (Stabilize `binary_heap_as_slice`) - rust-lang#124214 (Parse unsafe attributes) - rust-lang#125572 (Detect pub structs never constructed and unused associated constants) - rust-lang#125781 (prefer `compile::stream_cargo` for building tools) - rust-lang#126030 (Update `./x fmt` command in library/std/src/sys/pal/windows/c/README.md) - rust-lang#126047 (Simplify the rayon calls in the installer) - rust-lang#126089 (Stabilize Option::take_if) - rust-lang#126112 (Clean up source root in run-make tests) - rust-lang#126119 (Improve docs for using custom paths with `--emit`) r? `@ghost` `@rustbot` modify labels: rollup
…iaskrgr Rollup of 11 pull requests Successful merges: - rust-lang#124012 (Stabilize `binary_heap_as_slice`) - rust-lang#124214 (Parse unsafe attributes) - rust-lang#125572 (Detect pub structs never constructed and unused associated constants) - rust-lang#125781 (prefer `compile::stream_cargo` for building tools) - rust-lang#126030 (Update `./x fmt` command in library/std/src/sys/pal/windows/c/README.md) - rust-lang#126047 (Simplify the rayon calls in the installer) - rust-lang#126052 (More `rustc_parse` cleanups) - rust-lang#126077 (Revert "Use the HIR instead of mir_keys for determining whether something will have a MIR body.") - rust-lang#126089 (Stabilize Option::take_if) - rust-lang#126112 (Clean up source root in run-make tests) - rust-lang#126119 (Improve docs for using custom paths with `--emit`) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#124214 - carbotaniuman:parse_unsafe_attrs, r=michaelwoerister Parse unsafe attributes Initial parse implementation for rust-lang#123757 This is the initial work to parse unsafe attributes, which is represented as an extra `unsafety` field in `MetaItem` and `AttrItem`. There's two areas in the code where it appears that parsing is done manually and not using the parser stuff, and I'm not sure how I'm supposed to thread the change there.
…r, r=nnethercote Stabilize `unsafe_attributes` # Stabilization report ## Summary This is a tracking issue for the RFC 3325: unsafe attributes We are stabilizing `#![feature(unsafe_attributes)]`, which makes certain attributes considered 'unsafe', meaning that they must be surrounded by an `unsafe(...)`, as in `#[unsafe(no_mangle)]`. RFC: rust-lang/rfcs#3325 Tracking issue: rust-lang#123757 ## What is stabilized ### Summary of stabilization Certain attributes will now be designated as unsafe attributes, namely, `no_mangle`, `export_name`, and `link_section` (stable only), and these attributes will need to be called by surrounding them in `unsafe(...)` syntax. On editions prior to 2024, this is simply an edition lint, but it will become a hard error in 2024. This also works in `cfg_attr`, but `unsafe` is not allowed for any other attributes, including proc-macros ones. ```rust #[unsafe(no_mangle)] fn a() {} #[cfg_attr(any(), unsafe(export_name = "c"))] fn b() {} ``` For a table showing the attributes that were considered to be included in the list to require unsafe, and subsequent reasoning about why each such attribute was or was not included, see [this comment here](rust-lang#124214 (comment)) ## Tests The relevant tests are in `tests/ui/rust-2024/unsafe-attributes` and `tests/ui/attributes/unsafe`.
… r=nnethercote Stabilize `unsafe_attributes` # Stabilization report ## Summary This is a tracking issue for the RFC 3325: unsafe attributes We are stabilizing `#![feature(unsafe_attributes)]`, which makes certain attributes considered 'unsafe', meaning that they must be surrounded by an `unsafe(...)`, as in `#[unsafe(no_mangle)]`. RFC: rust-lang/rfcs#3325 Tracking issue: rust-lang#123757 ## What is stabilized ### Summary of stabilization Certain attributes will now be designated as unsafe attributes, namely, `no_mangle`, `export_name`, and `link_section` (stable only), and these attributes will need to be called by surrounding them in `unsafe(...)` syntax. On editions prior to 2024, this is simply an edition lint, but it will become a hard error in 2024. This also works in `cfg_attr`, but `unsafe` is not allowed for any other attributes, including proc-macros ones. ```rust #[unsafe(no_mangle)] fn a() {} #[cfg_attr(any(), unsafe(export_name = "c"))] fn b() {} ``` For a table showing the attributes that were considered to be included in the list to require unsafe, and subsequent reasoning about why each such attribute was or was not included, see [this comment here](rust-lang#124214 (comment)) ## Tests The relevant tests are in `tests/ui/rust-2024/unsafe-attributes` and `tests/ui/attributes/unsafe`.
…rcote Stabilize `unsafe_attributes` # Stabilization report ## Summary This is a tracking issue for the RFC 3325: unsafe attributes We are stabilizing `#![feature(unsafe_attributes)]`, which makes certain attributes considered 'unsafe', meaning that they must be surrounded by an `unsafe(...)`, as in `#[unsafe(no_mangle)]`. RFC: rust-lang/rfcs#3325 Tracking issue: #123757 ## What is stabilized ### Summary of stabilization Certain attributes will now be designated as unsafe attributes, namely, `no_mangle`, `export_name`, and `link_section` (stable only), and these attributes will need to be called by surrounding them in `unsafe(...)` syntax. On editions prior to 2024, this is simply an edition lint, but it will become a hard error in 2024. This also works in `cfg_attr`, but `unsafe` is not allowed for any other attributes, including proc-macros ones. ```rust #[unsafe(no_mangle)] fn a() {} #[cfg_attr(any(), unsafe(export_name = "c"))] fn b() {} ``` For a table showing the attributes that were considered to be included in the list to require unsafe, and subsequent reasoning about why each such attribute was or was not included, see [this comment here](rust-lang/rust#124214 (comment)) ## Tests The relevant tests are in `tests/ui/rust-2024/unsafe-attributes` and `tests/ui/attributes/unsafe`.
…rcote Stabilize `unsafe_attributes` # Stabilization report ## Summary This is a tracking issue for the RFC 3325: unsafe attributes We are stabilizing `#![feature(unsafe_attributes)]`, which makes certain attributes considered 'unsafe', meaning that they must be surrounded by an `unsafe(...)`, as in `#[unsafe(no_mangle)]`. RFC: rust-lang/rfcs#3325 Tracking issue: #123757 ## What is stabilized ### Summary of stabilization Certain attributes will now be designated as unsafe attributes, namely, `no_mangle`, `export_name`, and `link_section` (stable only), and these attributes will need to be called by surrounding them in `unsafe(...)` syntax. On editions prior to 2024, this is simply an edition lint, but it will become a hard error in 2024. This also works in `cfg_attr`, but `unsafe` is not allowed for any other attributes, including proc-macros ones. ```rust #[unsafe(no_mangle)] fn a() {} #[cfg_attr(any(), unsafe(export_name = "c"))] fn b() {} ``` For a table showing the attributes that were considered to be included in the list to require unsafe, and subsequent reasoning about why each such attribute was or was not included, see [this comment here](rust-lang/rust#124214 (comment)) ## Tests The relevant tests are in `tests/ui/rust-2024/unsafe-attributes` and `tests/ui/attributes/unsafe`.
…rcote Stabilize `unsafe_attributes` # Stabilization report ## Summary This is a tracking issue for the RFC 3325: unsafe attributes We are stabilizing `#![feature(unsafe_attributes)]`, which makes certain attributes considered 'unsafe', meaning that they must be surrounded by an `unsafe(...)`, as in `#[unsafe(no_mangle)]`. RFC: rust-lang/rfcs#3325 Tracking issue: #123757 ## What is stabilized ### Summary of stabilization Certain attributes will now be designated as unsafe attributes, namely, `no_mangle`, `export_name`, and `link_section` (stable only), and these attributes will need to be called by surrounding them in `unsafe(...)` syntax. On editions prior to 2024, this is simply an edition lint, but it will become a hard error in 2024. This also works in `cfg_attr`, but `unsafe` is not allowed for any other attributes, including proc-macros ones. ```rust #[unsafe(no_mangle)] fn a() {} #[cfg_attr(any(), unsafe(export_name = "c"))] fn b() {} ``` For a table showing the attributes that were considered to be included in the list to require unsafe, and subsequent reasoning about why each such attribute was or was not included, see [this comment here](rust-lang/rust#124214 (comment)) ## Tests The relevant tests are in `tests/ui/rust-2024/unsafe-attributes` and `tests/ui/attributes/unsafe`.
Initial parse implementation for #123757
This is the initial work to parse unsafe attributes, which is represented as an extra
unsafety
field inMetaItem
andAttrItem
. There's two areas in the code where it appears that parsing is done manually and not using the parser stuff, and I'm not sure how I'm supposed to thread the change there.