forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#68728 - Centril:towards-fn-merge, r=petroch…
…enkov parse: merge `fn` syntax + cleanup item parsing Here we continue the work in rust-lang#67131 in particular to merge the grammars of `fn` items in various positions. A list of *language level* changes (as sanctioned by the language team in rust-lang#65041 (comment) and rust-lang#67131): - `self` parameters are now *syntactically* allowed as the first parameter irrespective of item context (and in function pointers). Instead, semantic validation (`ast_validation`) is used. - Syntactically, `fn` items in `extern { ... }` blocks can now have bodies (`fn foo() { ... }` as opposed to `fn foo();`). As above, we use semantic restrictions instead. - Syntactically, `fn` items in free contexts (directly in a file or a module) can now be without bodies (`fn foo();` as opposed to `fn foo() { ... }`. As above, we use semantic restrictions instead, including for non-ident parameter patterns. - `const extern fn` feature gating is now done post-expansion such that we do not have conditional compatibilities of function qualifiers *in parsing*. - The `FnFrontMatter` grammar becomes: ```rust Extern = "extern" StringLit ; FnQual = "const"? "async"? "unsafe"? Extern? ; FnFrontMatter = FnQual "fn" ; ``` That is, all item contexts now *syntactically* allow `const async unsafe extern "C" fn` and use semantic restrictions to rule out combinations previously prevented syntactically. The semantic restrictions include in particular: - `fn`s in `extern { ... }` can have no qualifiers. - `const` and `async` cannot be combined. - To fuse the list-of-items parsing in the 4 contexts that items are allowed, we now must permit inner attributes (`#![attr]`) inside `trait Foo { ... }` definitions. That is, we now allow e.g. `trait Foo { #![attr] }`. This was probably an oversight due to not using a uniform parsing mechanism, which we now do have (`fn parse_item_list`). The semantic support (including e.g. for linting) falls out directly from the attributes infrastructure. To ensure this, we include a test for lints. Put together, these grammar changes allow us to substantially reduce the complexity of item parsing and its grammar. There are however some other non-language improvements that allow the compression to take place. A list of *compiler-internal* changes (in particular noting the parser-external data-structure changes): - We use `enum AllowPlus/RecoverQPath/AllowCVariadic { Yes, No }` in `parser/ty.rs` instead of passing around 3 different `bool`s. I felt this was necessary as it was becoming mentally taxing to track which-is-which. - `fn visit_trait_item` and `fn visit_impl_item` are merged into `fn visit_assoc_item` which now is passed an `AssocCtxt` to check which one it is. - We change `FnKind` to: ```rust pub enum FnKind<'a> { Fn(FnCtxt, Ident, &'a FnSig, &'a Visibility, Option<&'a Block>), Closure(&'a FnDecl, &'a Expr), } ``` with: ```rust pub enum FnCtxt { Free, Foreign, Assoc(AssocCtxt), } ``` This is then taken advantage of in tweaking the various semantic restrictions as well as in pretty printing. - In `ItemKind::Fn`, we change `P<Block>` to `Option<P<Block>>`. - In `ForeignItemKind::Fn`, we change `P<FnDecl>` to `FnSig` and `P<Block>` to `Option<P<Block>>`. - We change `ast::{Unsafety, Spanned<Constness>}>` into `enum ast::{Unsafe, Const} { Yes(Span), No }` respectively. This change in formulation allow us to exclude `Span` in the case of `No`, which facilitates parsing. Moreover, we also add a `Span` to `IsAsync` which is renamed to `Async`. The new `Span`s in `Unsafety` and `Async` are then taken advantage of for better diagnostics. A reason this change was made is to have a more uniform and clear naming scheme. The HIR keeps the structures in AST (with those definitions moved into HIR) for now to avoid regressing perf. - Various cleanups, bug fixes, and diagnostics improvements are made along the way. It is probably best to understand those via the diffs. I would recommend reviewing this commit-by-commit with whitespace changes hidden. r? @estebank @petrochenkov
- Loading branch information
Showing
47 changed files
with
596 additions
and
723 deletions.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// pp-exact | ||
|
||
trait Foo { | ||
#![allow(bar)] | ||
} | ||
|
||
fn main() { } |
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 |
---|---|---|
@@ -1,13 +1,11 @@ | ||
error: expected one of `async`, `const`, `crate`, `extern`, `fn`, `pub`, `static`, `type`, or `unsafe`, found keyword `let` | ||
--> $DIR/issue-54441.rs:3:9 | ||
error: missing `fn`, `type`, or `static` for extern-item declaration | ||
--> $DIR/issue-54441.rs:1:1 | ||
| | ||
LL | let | ||
| ^^^ expected one of 9 possible tokens | ||
... | ||
LL | m!(); | ||
| ----- in this macro invocation | ||
| | ||
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) | ||
LL | / macro_rules! m { | ||
LL | | | ||
LL | | () => { | ||
LL | | let | ||
| |________^ missing `fn`, `type`, or `static` | ||
|
||
error: aborting due to previous error | ||
|
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
error: expected item after attributes | ||
--> $DIR/attr-before-eof.rs:3:16 | ||
--> $DIR/attr-before-eof.rs:3:1 | ||
| | ||
LL | #[derive(Debug)] | ||
| ^ | ||
| ^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
error: expected item after attributes | ||
--> $DIR/attr-dangling-in-mod.rs:6:14 | ||
--> $DIR/attr-dangling-in-mod.rs:6:1 | ||
| | ||
LL | #[foo = "bar"] | ||
| ^ | ||
| ^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
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 |
---|---|---|
@@ -1,13 +1,7 @@ | ||
// Constants (static variables) can be used to match in patterns, but mutable | ||
// statics cannot. This ensures that there's some form of error if this is | ||
// attempted. | ||
// Make sure there's an error when given `extern { ... #[attr] }`. | ||
|
||
extern crate libc; | ||
fn main() {} | ||
|
||
extern { | ||
static mut rust_dbg_static_mut: libc::c_int; | ||
pub fn rust_dbg_static_mut_check_four(); | ||
#[cfg(stage37)] //~ ERROR expected item after attributes | ||
} | ||
|
||
pub fn main() {} |
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
error: expected item after attributes | ||
--> $DIR/attrs-after-extern-mod.rs:10:19 | ||
--> $DIR/attrs-after-extern-mod.rs:6:5 | ||
| | ||
LL | #[cfg(stage37)] | ||
| ^ | ||
| ^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
error: expected item after attributes | ||
--> $DIR/doc-before-attr.rs:4:16 | ||
--> $DIR/doc-before-attr.rs:4:1 | ||
| | ||
LL | /// hi | ||
| ------ other attributes here | ||
LL | #[derive(Debug)] | ||
| ^ | ||
| ^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
fn main() {} | ||
|
||
extern { | ||
/// hi | ||
//~^ ERROR expected item after doc comment | ||
//~^ ERROR found a documentation comment that doesn't document anything | ||
} |
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 |
---|---|---|
@@ -1,8 +1,11 @@ | ||
error: expected item after doc comment | ||
--> $DIR/doc-before-extern-rbrace.rs:2:5 | ||
error[E0584]: found a documentation comment that doesn't document anything | ||
--> $DIR/doc-before-extern-rbrace.rs:4:5 | ||
| | ||
LL | /// hi | ||
| ^^^^^^ this doc comment doesn't document anything | ||
| | ||
= help: doc comments must come before what they document, maybe a comment was intended with `//`? | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0584`. |
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
// error-pattern: expected one of `(`, `async`, `const`, `extern`, `fn` | ||
fn main() {} | ||
|
||
extern { | ||
pub pub fn foo(); | ||
//~^ ERROR missing `fn`, `type`, or `static` for extern-item declaration | ||
} |
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
error: expected one of `(`, `async`, `const`, `extern`, `fn`, `static`, `type`, or `unsafe`, found keyword `pub` | ||
--> $DIR/duplicate-visibility.rs:3:9 | ||
error: missing `fn`, `type`, or `static` for extern-item declaration | ||
--> $DIR/duplicate-visibility.rs:4:8 | ||
| | ||
LL | pub pub fn foo(); | ||
| ^^^ expected one of 8 possible tokens | ||
| ^ missing `fn`, `type`, or `static` | ||
|
||
error: aborting due to previous error | ||
|
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// check-pass | ||
|
||
#![deny(non_camel_case_types)] | ||
|
||
fn main() {} | ||
|
||
trait foo_bar { | ||
#![allow(non_camel_case_types)] | ||
} |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
trait T { | ||
extern "Rust" unsafe fn foo(); //~ ERROR expected one of `async`, `const` | ||
//~^ ERROR missing `fn`, `type`, or `const` for associated-item declaration | ||
extern "Rust" unsafe fn foo(); | ||
} | ||
|
||
fn main() {} |
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 |
---|---|---|
@@ -1,10 +1,11 @@ | ||
error: expected one of `async`, `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, `unsafe`, or `}`, found keyword `extern` | ||
--> $DIR/issue-19398.rs:2:5 | ||
error: missing `fn`, `type`, or `const` for associated-item declaration | ||
--> $DIR/issue-19398.rs:1:10 | ||
| | ||
LL | trait T { | ||
| - expected one of 10 possible tokens | ||
LL | extern "Rust" unsafe fn foo(); | ||
| ^^^^^^ unexpected token | ||
LL | trait T { | ||
| __________^ | ||
LL | | | ||
LL | | extern "Rust" unsafe fn foo(); | ||
| |____^ missing `fn`, `type`, or `const` | ||
|
||
error: aborting due to previous error | ||
|
Oops, something went wrong.