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

macro parser should back out of failed nonterminal parsing #3232

Closed
paulstansifer opened this issue Aug 20, 2012 · 11 comments
Closed

macro parser should back out of failed nonterminal parsing #3232

paulstansifer opened this issue Aug 20, 2012 · 11 comments
Labels
A-parser Area: The parsing of Rust source code to an AST A-syntaxext Area: Syntax extensions P-low Low priority

Comments

@paulstansifer
Copy link
Contributor

Currently, the following macro argument grammar will not successfully parse anything, complaining of a "local ambiguity":

 $( $t:ty )* #

The problem is that if it were to use the Rust parser to try to parse a type and fail, the whole task would be killed. This is easy to fix after that problem is resolved. There are two options:

  1. Break parsing into a separate task, and see whether it survives. However, parse.rs requires a parse_sess, which is mutable state, and can't be shared. So the second option is probably more doable:
  2. Allow the parser to safely return instead of killing the task. This works much like the Error monad. Ordinarily, this would require a lot of tedious wrapping and unwrapping, but we have a macro system! It should be relatively easy to make a notation like Haskell's do, and the parser would be able to stay in the standard imperative style.
@eholk
Copy link
Contributor

eholk commented Aug 22, 2012

+1

@nikomatsakis
Copy link
Contributor

Is there an option (3)---require that the grammar be LALR(k) or something like that? Or does that fundamentally not work?

@nikomatsakis
Copy link
Contributor

That said: we should do (2) anyway, because it's lame that we cannot recover what-so-ever from parse failures.

@paulstansifer
Copy link
Contributor Author

The macro parser doesn't do any lookahead at all, and LR(0) is probably too
restrictive. So it's probably not a good idea to build in such a
restriction.

@graydon
Copy link
Contributor

graydon commented Mar 22, 2013

non-critical for 0.6, de-milestoning

@graydon
Copy link
Contributor

graydon commented Apr 25, 2013

nominating for backwards-compatible milestone. though if we agree to do (2) here maybe we can push this to production ready.

@catamorphism
Copy link
Contributor

Adding nominated label, since I assume @graydon intended to nominate it to change the milestone.

@graydon
Copy link
Contributor

graydon commented Jul 18, 2013

accepted for production-ready milestone

@vadimcn
Copy link
Contributor

vadimcn commented Sep 20, 2013

Option (4): Break parsing into a separate task and pass parse_sess to it unsafely.

@pnkfelix
Copy link
Member

`accepted for P-low.

@paulstansifer paulstansifer self-assigned this May 11, 2014
@paulstansifer paulstansifer removed their assignment Jun 16, 2014
vadimcn added a commit to vadimcn/rust that referenced this issue Oct 7, 2014
…g options..." error in macros, which often occurs when trying to match parts of Rust syntax.

For example, this matcher: `fn $name:ident( $($param:ident : $pty:ty),* )` would fail when parsing `fn foo()`, because macro parser wouldn't realize that an ident cannot start with `)`.

This resolves rust-lang#5902, and at least partially mitigates rust-lang#9364 and rust-lang#3232.
@steveklabnik
Copy link
Member

Given that this is mentioned in rust-lang/rfcs#440, I'm going to be closing as 'not a bug' at this time, anyway. If we decide to improve macros, this is something that'd be nice to fix, for sure, but that RFC issue will be where that happens.

bors pushed a commit to rust-lang-ci/rust that referenced this issue May 15, 2021
RalfJung pushed a commit to RalfJung/rust that referenced this issue Dec 26, 2023
jaisnan pushed a commit to jaisnan/rust-dev that referenced this issue Jul 29, 2024
Add support for History Expressions. This allows an ensures clause to
evaluate expressions before the function runs, bing them to anonymous
variables and use them after calculating the result to be able to
compare to the state before the function evaluation.

Resolves rust-lang#2803

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.

---------

Co-authored-by: Matias Scharager <[email protected]>
Co-authored-by: Justus Adam <[email protected]>
Co-authored-by: Felipe R. Monteiro <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-parser Area: The parsing of Rust source code to an AST A-syntaxext Area: Syntax extensions P-low Low priority
Projects
None yet
Development

No branches or pull requests

8 participants