mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-11 22:43:42 +00:00
Merge #3805
3805: lower literal patterns r=JoshMcguigan a=JoshMcguigan While working on #3706 I discovered literal patterns weren't being lowered. This PR implements that lowering. Questions for reviewers: 1. This re-uses the existing conversion from `ast::LiteralKind` to `Literal`, but `ast::LiteralKind` doesn't include information about the actual value of the literal, which causes `Literal` to be created with the default value for the type (rather than the actual value in the source code). Am I correct in thinking that we'd eventually want to change things in such a way that we could initialize the `Literal` with the actual literal value? Is there an existing issue for this, or else perhaps I should create one to discuss how it should be implemented? My main question would be whether `ast::LiteralKind` should be extended to hold the actual value, or if we should provide some other way to get that information from `ast::Literal`? 2. I couldn't find tests which directly cover this, but it does seem to work in #3706. Do we have unit tests for this lowering code? 3. I'm not sure why `lit.literal()` returns an `Option`. Is returning a `Pat::Missing` in the `None` case the right thing to do? 4. I was basically practicing type-system driven development to figure out the transformation from `ast::Pat::LiteralPat` to `Pat::Lit`. I don't have an immediate question here, but I just wanted to ensure this section is looked at closely during review. Co-authored-by: Josh Mcguigan <joshmcg88@gmail.com>
This commit is contained in:
commit
67351a011b
@ -417,26 +417,7 @@ impl ExprCollector<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
ast::Expr::Literal(e) => {
|
||||
let lit = match e.kind() {
|
||||
LiteralKind::IntNumber { suffix } => {
|
||||
let known_name = suffix.and_then(|it| BuiltinInt::from_suffix(&it));
|
||||
|
||||
Literal::Int(Default::default(), known_name)
|
||||
}
|
||||
LiteralKind::FloatNumber { suffix } => {
|
||||
let known_name = suffix.and_then(|it| BuiltinFloat::from_suffix(&it));
|
||||
|
||||
Literal::Float(Default::default(), known_name)
|
||||
}
|
||||
LiteralKind::ByteString => Literal::ByteString(Default::default()),
|
||||
LiteralKind::String => Literal::String(Default::default()),
|
||||
LiteralKind::Byte => Literal::Int(Default::default(), Some(BuiltinInt::U8)),
|
||||
LiteralKind::Bool => Literal::Bool(Default::default()),
|
||||
LiteralKind::Char => Literal::Char(Default::default()),
|
||||
};
|
||||
self.alloc_expr(Expr::Literal(lit), syntax_ptr)
|
||||
}
|
||||
ast::Expr::Literal(e) => self.alloc_expr(Expr::Literal(e.kind().into()), syntax_ptr),
|
||||
ast::Expr::IndexExpr(e) => {
|
||||
let base = self.collect_expr_opt(e.base());
|
||||
let index = self.collect_expr_opt(e.index());
|
||||
@ -679,10 +660,19 @@ impl ExprCollector<'_> {
|
||||
suffix: suffix.into_iter().map(|p| self.collect_pat(p)).collect(),
|
||||
}
|
||||
}
|
||||
ast::Pat::LiteralPat(lit) => {
|
||||
if let Some(ast_lit) = lit.literal() {
|
||||
let expr = Expr::Literal(ast_lit.kind().into());
|
||||
let expr_ptr = AstPtr::new(&ast::Expr::Literal(ast_lit));
|
||||
let expr_id = self.alloc_expr(expr, expr_ptr);
|
||||
Pat::Lit(expr_id)
|
||||
} else {
|
||||
Pat::Missing
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: implement
|
||||
ast::Pat::BoxPat(_) => Pat::Missing,
|
||||
ast::Pat::LiteralPat(_) => Pat::Missing,
|
||||
ast::Pat::RangePat(_) => Pat::Missing,
|
||||
};
|
||||
let ptr = AstPtr::new(&pat);
|
||||
@ -741,3 +731,25 @@ impl From<ast::BinOp> for BinaryOp {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ast::LiteralKind> for Literal {
|
||||
fn from(ast_lit_kind: ast::LiteralKind) -> Self {
|
||||
match ast_lit_kind {
|
||||
LiteralKind::IntNumber { suffix } => {
|
||||
let known_name = suffix.and_then(|it| BuiltinInt::from_suffix(&it));
|
||||
|
||||
Literal::Int(Default::default(), known_name)
|
||||
}
|
||||
LiteralKind::FloatNumber { suffix } => {
|
||||
let known_name = suffix.and_then(|it| BuiltinFloat::from_suffix(&it));
|
||||
|
||||
Literal::Float(Default::default(), known_name)
|
||||
}
|
||||
LiteralKind::ByteString => Literal::ByteString(Default::default()),
|
||||
LiteralKind::String => Literal::String(Default::default()),
|
||||
LiteralKind::Byte => Literal::Int(Default::default(), Some(BuiltinInt::U8)),
|
||||
LiteralKind::Bool => Literal::Bool(Default::default()),
|
||||
LiteralKind::Char => Literal::Char(Default::default()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user