parse: merge fn
syntax + cleanup item parsing by Centril · Pull Request #68728 · rust-lang/rust (original) (raw)
Conversation
This file contains hidden or 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
[ Show hidden characters]({{ revealButtonHref }})
Relevant to the language team, which will review and decide on the PR/issue.
Marks issues that should be documented in the release notes of the next release.
labels
Centril added a commit to Centril/rust that referenced this pull request
parser: syntactically allow self
in all fn
contexts
Part of rust-lang#68728.
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.
Centril added a commit to Centril/rust that referenced this pull request
parser: syntactically allow self
in all fn
contexts
Part of rust-lang#68728.
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.
Centril added a commit to Centril/rust that referenced this pull request
…henkov
Towards unified fn
grammar
Part of rust-lang#68728.
Syntactically,
fn
items inextern { ... }
blocks can now have bodies (fn foo() { ... }
as opposed tofn 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 tofn foo() { ... }
. As above, we use semantic restrictions instead, including for non-ident parameter patterns.We move towards unifying the
fn
front matter; this is fully realized in rust-lang#68728.
Centril added a commit to Centril/rust that referenced this pull request
…henkov
Towards unified fn
grammar
Part of rust-lang#68728.
Syntactically,
fn
items inextern { ... }
blocks can now have bodies (fn foo() { ... }
as opposed tofn 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 tofn foo() { ... }
. As above, we use semantic restrictions instead, including for non-ident parameter patterns.We move towards unifying the
fn
front matter; this is fully realized in rust-lang#68728.
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request
…henkov
Towards unified fn
grammar
Part of rust-lang#68728.
Syntactically,
fn
items inextern { ... }
blocks can now have bodies (fn foo() { ... }
as opposed tofn 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 tofn foo() { ... }
. As above, we use semantic restrictions instead, including for non-ident parameter patterns.We move towards unifying the
fn
front matter; this is fully realized in rust-lang#68728.
Centril added S-waiting-on-review
Status: Awaiting review from the assignee but also interested parties.
and removed S-waiting-on-author
Status: This is awaiting some action (such as code changes or more information) from the author.
labels
bors added S-waiting-on-bors
Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
and removed S-waiting-on-review
Status: Awaiting review from the assignee but also interested parties.
labels
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request
…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 inextern { ... }
blocks can now have bodies (fn foo() { ... }
as opposed tofn 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 tofn 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: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 inextern { ... }
can have no qualifiers.const
andasync
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]
) insidetrait 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 }
inparser/ty.rs
instead of passing around 3 differentbool
s. I felt this was necessary as it was becoming mentally taxing to track which-is-which.fn visit_trait_item
andfn visit_impl_item
are merged intofn visit_assoc_item
which now is passed anAssocCtxt
to check which one it is.We change
FnKind
to:pub enum FnKind<'a> { Fn(FnCtxt, Ident, &'a FnSig, &'a Visibility, Option<&'a Block>), Closure(&'a FnDecl, &'a Expr), }
with:
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 changeP<Block>
toOption<P<Block>>
.In
ForeignItemKind::Fn
, we changeP<FnDecl>
toFnSig
andP<Block>
toOption<P<Block>>
.We change
ast::{Unsafety, Spanned<Constness>}>
intoenum ast::{Unsafe, Const} { Yes(Span), No }
respectively. This change in formulation allow us to excludeSpan
in the case ofNo
, which facilitates parsing. Moreover, we also add aSpan
toIsAsync
which is renamed toAsync
. The newSpan
s inUnsafety
andAsync
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.
bors added a commit that referenced this pull request
Rollup of 9 pull requests
Successful merges:
- #68728 (parse: merge
fn
syntax + cleanup item parsing) - #68938 (fix lifetime shadowing check in GATs)
- #69057 (expand: misc cleanups and simplifications)
- #69108 (Use HirId in TraitCandidate.)
- #69125 (Add comment to SGX entry code)
- #69126 (miri: fix exact_div)
- #69127 (Enable use after scope detection in the new LLVM pass manager)
- #69135 (Spelling error "represening" to "representing")
- #69141 (Don't error on network failures)
Failed merges:
r? @ghost
ehuss mentioned this pull request
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request
Pkgsrc changes:
- Bump rust bootstrap version to 1.42.0, except for Darwin/i686 where the bootstrap is not (yet?) available.
Upstream changes:
Version 1.43.0 (2020-04-23)
Language
- Fixed using binary operations with
&{number}
(e.g.&1.0
) not having the type inferred correctly. - Attributes such as
#[cfg()]
can now be used onif
expressions.
Syntax only changes
- Allow
type Foo: Ord
syntactically. - Fuse associated and extern items up to defaultness.
- Syntactically allow
self
in allfn
contexts. - Merge
fn
syntax + cleanup item parsing. item
macro fragments can be interpolated intotrait
s,impl
s, andextern
blocks. For example, you may now write:macro_rules! mac_trait { ($i:item) => { trait T { $i } } } mac_trait! { fn foo() {} }
These are still rejected semantically, so you will likely receive an error but these changes can be seen and parsed by macros and conditional compilation.
Compiler
- You can now pass multiple lint flags to rustc to override the previous
flags. For example;
rustc -D unused -A unused-variables
denies everything in theunused
lint group exceptunused-variables
which is explicitly allowed. However, passingrustc -A unused-variables -D unused
denies everything in theunused
lint group includingunused-variables
since the allow flag is specified before the deny flag (and therefore overridden). - rustc will now prefer your system MinGW libraries over its bundled libraries
if they are available on
windows-gnu
. - rustc now buffers errors/warnings printed in JSON.
Libraries
Arc<[T; N]>
,Box<[T; N]>
, andRc<[T; N]>
, now implementTryFrom<Arc<[T]>>
,TryFrom<Box<[T]>>
, andTryFrom<Rc<[T]>>
respectively. Note These conversions are only available whenN
is0..=32
.- You can now use associated constants on floats and integers directly, rather
than having to import the module. e.g. You can now write
u32::MAX
orf32::NAN
with no imports. u8::is_ascii
is nowconst
.String
now implementsAsMut<str>
.- Added the
primitive
module tostd
andcore
. This module reexports Rust's primitive types. This is mainly useful in macros where you want avoid these types being shadowed. - Relaxed some of the trait bounds on
HashMap
andHashSet
. string::FromUtf8Error
now implementsClone + Eq
.
Stabilized APIs
Cargo
- You can now set config
[profile]
s in your.cargo/config
, or through your environment. - Cargo will now set
CARGO_BIN_EXE_<name>
pointing to a binary's executable path when running integration tests or benchmarks.<name>
is the name of your binary as-is e.g. If you wanted the executable path for a binary namedmy-program
you would useenv!("CARGO_BIN_EXE_my-program")
.
Misc
- Certain checks in the
const_err
lint were deemed unrelated to const evaluation, and have been moved to theunconditional_panic
andarithmetic_overflow
lints.
Compatibility Notes
- Having trailing syntax in the
assert!
macro is now a hard error. This has been a warning since 1.36.0. - Fixed
Self
not having the correctly inferred type. This incorrectly led to some instances being accepted, and now correctly emits a hard error.
Internal Only
These changes provide no direct user facing benefits, but represent significant
improvements to the internals and overall performance of rustc
and
related tools.
- All components are now built with
opt-level=3
instead of2
. - Improved how rustc generates drop code.
- Improved performance from
#[inline]
-ing certain hot functions. - traits: preallocate 2 Vecs of known initial size
- Avoid exponential behaviour when relating types
- Skip
Drop
terminators for enum variants without drop glue - Improve performance of coherence checks
- Deduplicate types in the generator witness
- Invert control in struct_lint_level.
ehuss mentioned this pull request
ehuss mentioned this pull request