Skip to content

Fix parsing for casting an empty return expression - #39303

Closed
cramertj wants to merge 1 commit into
rust-lang:masterfrom
cramertj:cramertj/parse_return_as
Closed

cramertj wants to merge 1 commit into
rust-lang:masterfrom
cramertj:cramertj/parse_return_as

Conversation

@cramertj

Copy link
Copy Markdown
Member

Partial fix for #28784.

@rust-highfive

Copy link
Copy Markdown
Contributor

r? @nikomatsakis

(rust_highfive has picked a reviewer for you, use r? to override)

@petrochenkov

petrochenkov commented Jan 25, 2017 •

Copy link
Copy Markdown
Contributor

Hm, I think you need to

  • add the same condition to NtIdent below
  • actually list all possible ident that can start an expression.

The list should look like this if I'm not missing anything:

  • all !is_any_keyword idents
  • is_path_segment_keyword idents
  • box, break, continue, false, for, if, loop, match, move, return, true, unsafe, while

@cramertj

cramertj commented Jan 25, 2017 via email

Copy link
Copy Markdown
Member Author

@petrochenkov

petrochenkov commented Jan 25, 2017 •

Copy link
Copy Markdown
Contributor

@cramertj
Making can_begin_expr precise is the general solution for the return sub-problem of #28784 (it affects couple of other places as well), so it's wanted.

@cramertj

Copy link
Copy Markdown
Member Author

@petrochenkov Great! I'll make that change then.

@cramertj cramertj closed this Jan 26, 2017
@cramertj
cramertj deleted the cramertj/parse_return_as branch January 26, 2017 00:25
@cramertj

Copy link
Copy Markdown
Member Author

@petrochenkov Just started working on this, and I wanted to clarify the desired behavior for the NtIdent case. Currently, is_keyword, is_any_keyword, is_strict_keyword, etc. return false for Interpolated(NtIdent(..)) (or rather, they return false for all non-Idents). Should that be changed, too?

@petrochenkov

petrochenkov commented Jan 26, 2017 •

Copy link
Copy Markdown
Contributor

@cramertj
Actually, disregard my comment about NtIdent, it looks like macro machinery always unwraps it into Ident immediately, so it can never be encountered during parsing.

@cramertj

Copy link
Copy Markdown
Member Author

@petrochenkov
Does this look right to you?

fn ident_can_begin_expr(ident: ast::Ident) -> bool {
    let ident_token: Token = Ident(ident);

    !ident_token.is_any_keyword() ||
    ident_token.is_path_segment_keyword() ||
    [
        keywords::Box.name(),
        keywords::Break.name(),
        keywords::Continue.name(),
        keywords::False.name(),
        keywords::For.name(),
        keywords::If.name(),
        keywords::Loop.name(),
        keywords::Match.name(),
        keywords::Move.name(),
        keywords::Return.name(),
        keywords::True.name(),
        keywords::Unsafe.name(),
        keywords::While.name(),
    ].contains(&ident.name)
}

@cramertj

Copy link
Copy Markdown
Member Author

I don't think while and for can be expressions, can they? I'm guessing you added those because of the discussions around breaking with a value.

@petrochenkov

Copy link
Copy Markdown
Contributor

@cramertj

let a: () = while true { break };

They are expressions, they are just always result in () at the moment.

@cramertj

Copy link
Copy Markdown
Member Author

Huh. TIL.

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.

4 participants