Skip to content

Conversation

@JoshMcguigan
Copy link
Contributor

Following up on #3805, this PR adds the literal value to ast::LiteralKind so when we lower we can use the actual value from the source code rather than the default value for the type. Ultimately I plan to use this for exhaustiveness checking in #3706.

I didn't include this in the previous PR because I wasn't sure if it made sense to add this information to ast::LiteralKind or provide some other mechanism to get this from ast::Literal.

For now I've only implemented this for boolean literals, but I think it could be easily extended to other types. A possible exception to this are string literals, since we may not want to clone around an owned string to hold onto in ast::LiteralKind, and it'd be nice to avoid adding a generic lifetime as well. Perhaps we won't ever care about the actual value of a string literal?

@kjeremy
Copy link
Contributor

kjeremy commented Apr 1, 2020

Hm! I wonder if we could use this for graying out paths that are known to not be taken or something (eg if true {} else {}).

@Veetaha
Copy link
Contributor

Veetaha commented Apr 1, 2020

I think we can. Going further, e.g. TypeScript uses literal or unit (meaning single-value) types to bring more type-safety and contextual typing. But, this should actually be the concern of rustc in the first place...

Copy link
Member

@flodiebold flodiebold left a comment

Choose a reason for hiding this comment

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

bors r+

@bors
Copy link
Contributor

bors bot commented Apr 1, 2020

@bors bors bot merged commit 0a41412 into rust-lang:master Apr 1, 2020
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