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

Add AST validation pass and move some checks to it #33794

Merged
merged 3 commits into from
Jun 1, 2016

Conversation

petrochenkov
Copy link
Contributor

The purpose of this pass is to catch constructions that fit into AST data structures, but not permitted by the language. As an example, impls don't have visibilities, but for convenience and uniformity with other items they are represented with a structure Item which has Visibility field.

This pass is intended to run after expansion of macros and syntax extensions (and before lowering to HIR), so it can catch erroneous constructions that were generated by them. This pass allows to remove ad hoc semantic checks from the parser, which can be overruled by syntax extensions and occasionally macros.

The checks can be put here if they are simple, local, don't require results of any complex analysis like name resolution or type checking and maybe don't logically fall into other passes. I expect most of errors generated by this pass to be non-fatal and allowing the compilation to proceed.

I intend to move some more checks to this pass later and maybe extend it with new checks, like, for example, identifier validity. Given that syntax extensions are going to be stabilized in the measurable future, it's important that they would not be able to subvert usual language rules.

In this patch I've added two new checks - a check for labels named 'static and a check for lifetimes and labels named '_. The first one gives a hard error, the second one - a future compatibility warning.
Fixes #33059 ([breaking-change])
cc rust-lang/rfcs#1177

r? @nrc

pub impl Tr for S { //~ ERROR invalid visibility qualifier
pub fn f() {} //~ ERROR invalid visibility qualifier
pub const C: u8 = 0; //~ ERROR invalid visibility qualifier
pub type T = u8; //~ ERROR invalid visibility qualifier
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems to be for the worse, since pub is a valid qualifier, just unneccessary here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "unnecessary -> invalid" change is rolled back.

@nrc
Copy link
Member

nrc commented May 23, 2016

What is the value in having this as a separate pass, rather than being part of the AST->HIR lowering? One benefit seems to be separation of concerns, however, I can imagine wanting to assert a lot of the things checked by this pass in the lowering in any case, so that may be moot. Also, this should always be a small pass, since anything big will probably be better performed on the HIR, so it doesn't seem to bad to smoosh it together with HIR lowering.

@petrochenkov
Copy link
Contributor Author

petrochenkov commented May 23, 2016

@nrc

One benefit seems to be separation of concerns

Pretty much this. Mixing lowering code (almost always successfull) with AST validation code (intended solely for error reporting) looks kind of messy. It's not a strong opinion though.

I can imagine wanting to assert a lot of the things checked by this pass in the lowering in any case

I wouldn't expect that, later compiler passes are mostly ready to deal with anything they can face in non-validated AST/HIR, so the lowering step can be faultless. I expect the larger part of errors generated by the validation pass to be non-fatal (for example the restriction on pub impl is totally artificial, later passes don't care about presence or absence of this pub), so we can log them and continue compilation. If some fatal errors are moved to the validation pass (for example, a check for unexpanded macros), they can can abort compilation right away without reaching lowering code, so it won't have to use asserts for them too (just ignore ast::Macro things).

Hm, maybe I should rename ast_sanity to ast_validation...

@nrc
Copy link
Member

nrc commented May 24, 2016

r=me with sanity =-> validation

@petrochenkov petrochenkov changed the title Add AST sanity checking pass and move some checks to it Add AST validation pass and move some checks to it May 24, 2016
@petrochenkov
Copy link
Contributor Author

Updated.

@nrc
Copy link
Member

nrc commented May 26, 2016

@bors: r+

@bors
Copy link
Contributor

bors commented May 26, 2016

📌 Commit 91d8a4f has been approved by nrc

@bors
Copy link
Contributor

bors commented May 26, 2016

⌛ Testing commit 91d8a4f with merge fedc0f8...

bors added a commit that referenced this pull request May 26, 2016
Add AST validation pass and move some checks to it

The purpose of this pass is to catch constructions that fit into AST data structures, but not permitted by the language. As an example, `impl`s don't have visibilities, but for convenience and uniformity with other items they are represented with a structure `Item` which has `Visibility` field.

This pass is intended to run after expansion of macros and syntax extensions (and before lowering to HIR), so it can catch erroneous constructions that were generated by them. This pass allows to remove ad hoc semantic checks from the parser, which can be overruled by syntax extensions and occasionally macros.

The checks can be put here if they are simple, local, don't require results of any complex analysis like name resolution or type checking and maybe don't logically fall into other passes. I expect most of errors generated by this pass to be non-fatal and allowing the compilation to proceed.

I intend to move some more checks to this pass later and maybe extend it with new checks, like, for example, identifier validity. Given that syntax extensions are going to be stabilized in the measurable future, it's important that they would not be able to subvert usual language rules.

In this patch I've added two new checks - a check for labels named `'static` and a check for lifetimes and labels named `'_`. The first one gives a hard error, the second one - a future compatibility warning.
Fixes #33059 ([breaking-change])
cc rust-lang/rfcs#1177

r? @nrc
@bors bors mentioned this pull request May 27, 2016
@bors
Copy link
Contributor

bors commented May 27, 2016

💔 Test failed - auto-linux-64-opt-rustbuild

@petrochenkov
Copy link
Contributor Author

Updated.
Tidy check for Cargo.lock doesn't work on Windows for some reason.

@bors
Copy link
Contributor

bors commented May 27, 2016

☔ The latest upstream changes (presumably #33864) made this pull request unmergeable. Please resolve the merge conflicts.

@petrochenkov
Copy link
Contributor Author

Rebased.

@petrochenkov
Copy link
Contributor Author

ping @nrc

@nrc
Copy link
Member

nrc commented Jun 1, 2016

@bors: r=nrc

@bors
Copy link
Contributor

bors commented Jun 1, 2016

📌 Commit 731144b has been approved by nrc

@bors
Copy link
Contributor

bors commented Jun 1, 2016

⌛ Testing commit 731144b with merge c2cab1f...

bors added a commit that referenced this pull request Jun 1, 2016
Add AST validation pass and move some checks to it

The purpose of this pass is to catch constructions that fit into AST data structures, but not permitted by the language. As an example, `impl`s don't have visibilities, but for convenience and uniformity with other items they are represented with a structure `Item` which has `Visibility` field.

This pass is intended to run after expansion of macros and syntax extensions (and before lowering to HIR), so it can catch erroneous constructions that were generated by them. This pass allows to remove ad hoc semantic checks from the parser, which can be overruled by syntax extensions and occasionally macros.

The checks can be put here if they are simple, local, don't require results of any complex analysis like name resolution or type checking and maybe don't logically fall into other passes. I expect most of errors generated by this pass to be non-fatal and allowing the compilation to proceed.

I intend to move some more checks to this pass later and maybe extend it with new checks, like, for example, identifier validity. Given that syntax extensions are going to be stabilized in the measurable future, it's important that they would not be able to subvert usual language rules.

In this patch I've added two new checks - a check for labels named `'static` and a check for lifetimes and labels named `'_`. The first one gives a hard error, the second one - a future compatibility warning.
Fixes #33059 ([breaking-change])
cc rust-lang/rfcs#1177

r? @nrc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants