3806: lower bool literal value r=flodiebold a=JoshMcguigan

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? 

Co-authored-by: Josh Mcguigan <joshmcg88@gmail.com>
This commit is contained in:
bors[bot] 2020-04-01 17:38:00 +00:00 committed by GitHub
commit 0a41412ced
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 3 deletions

View file

@ -748,7 +748,7 @@ impl From<ast::LiteralKind> for Literal {
LiteralKind::ByteString => Literal::ByteString(Default::default()), LiteralKind::ByteString => Literal::ByteString(Default::default()),
LiteralKind::String => Literal::String(Default::default()), LiteralKind::String => Literal::String(Default::default()),
LiteralKind::Byte => Literal::Int(Default::default(), Some(BuiltinInt::U8)), LiteralKind::Byte => Literal::Int(Default::default(), Some(BuiltinInt::U8)),
LiteralKind::Bool => Literal::Bool(Default::default()), LiteralKind::Bool(val) => Literal::Bool(val),
LiteralKind::Char => Literal::Char(Default::default()), LiteralKind::Char => Literal::Char(Default::default()),
} }
} }

View file

@ -308,7 +308,7 @@ pub enum LiteralKind {
Byte, Byte,
IntNumber { suffix: Option<SmolStr> }, IntNumber { suffix: Option<SmolStr> },
FloatNumber { suffix: Option<SmolStr> }, FloatNumber { suffix: Option<SmolStr> },
Bool, Bool(bool),
} }
impl ast::Literal { impl ast::Literal {
@ -355,7 +355,8 @@ impl ast::Literal {
LiteralKind::FloatNumber { suffix: Self::find_suffix(&text, &FLOAT_SUFFIXES) } LiteralKind::FloatNumber { suffix: Self::find_suffix(&text, &FLOAT_SUFFIXES) }
} }
STRING | RAW_STRING => LiteralKind::String, STRING | RAW_STRING => LiteralKind::String,
T![true] | T![false] => LiteralKind::Bool, T![true] => LiteralKind::Bool(true),
T![false] => LiteralKind::Bool(false),
BYTE_STRING | RAW_BYTE_STRING => LiteralKind::ByteString, BYTE_STRING | RAW_BYTE_STRING => LiteralKind::ByteString,
CHAR => LiteralKind::Char, CHAR => LiteralKind::Char,
BYTE => LiteralKind::Byte, BYTE => LiteralKind::Byte,