From ce37f3360f2640fa29f94e9a28e04d11387b9725 Mon Sep 17 00:00:00 2001 From: Nika Layzell Date: Tue, 5 Dec 2017 12:01:22 -0500 Subject: [PATCH] Support literals in const generics without a block --- src/gen/fold.rs | 2 +- src/gen/visit.rs | 2 +- src/gen/visit_mut.rs | 2 +- src/ty.rs | 25 +++++++++++++++++++------ 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/gen/fold.rs b/src/gen/fold.rs index ed7db7c93d..08d0b41e87 100644 --- a/src/gen/fold.rs +++ b/src/gen/fold.rs @@ -1486,7 +1486,7 @@ pub fn fold_generic_argument(_visitor: &mut V, _i: GenericAr } Const(_binding_0, ) => { Const ( - full!(_visitor.fold_expr_block(_binding_0)), + _visitor.fold_expr(_binding_0), ) } } diff --git a/src/gen/visit.rs b/src/gen/visit.rs index b91254f545..e9f99e3677 100644 --- a/src/gen/visit.rs +++ b/src/gen/visit.rs @@ -1156,7 +1156,7 @@ pub fn visit_generic_argument<'ast, V: Visitor<'ast> + ?Sized>(_visitor: &mut V, _visitor.visit_type_binding(&* _binding_0); } Const(ref _binding_0, ) => { - full!(_visitor.visit_expr_block(&* _binding_0)); + _visitor.visit_expr(&* _binding_0); } } } diff --git a/src/gen/visit_mut.rs b/src/gen/visit_mut.rs index 5ff8dce5a1..9ce6c421a4 100644 --- a/src/gen/visit_mut.rs +++ b/src/gen/visit_mut.rs @@ -1156,7 +1156,7 @@ pub fn visit_generic_argument_mut(_visitor: &mut V, _i: _visitor.visit_type_binding_mut(&mut * _binding_0); } Const(ref mut _binding_0, ) => { - full!(_visitor.visit_expr_block_mut(&mut * _binding_0)); + _visitor.visit_expr_mut(&mut * _binding_0); } } } diff --git a/src/ty.rs b/src/ty.rs index 085bf9cd4c..dd6c206fd4 100644 --- a/src/ty.rs +++ b/src/ty.rs @@ -205,7 +205,7 @@ ast_enum! { /// /// NOTE: Identity expressions are represented as Type arguments, as /// they are indistinguishable syntactically. - Const(ExprBlock), + Const(Expr), } } @@ -716,7 +716,9 @@ pub mod parsing { | syn!(TypeBinding) => { GenericArgument::TypeBinding } | - syn!(ExprBlock) => { GenericArgument::Const } + syn!(Lit) => { |l| GenericArgument::Const(ExprKind::Lit(l).into()) } + | + syn!(ExprBlock) => { |b| GenericArgument::Const(ExprKind::Block(b).into()) } )); } @@ -1048,10 +1050,21 @@ mod printing { GenericArgument::Lifetime(ref lt) => lt.to_tokens(tokens), GenericArgument::Type(ref ty) => ty.to_tokens(tokens), GenericArgument::TypeBinding(ref tb) => tb.to_tokens(tokens), - #[cfg(not(feature = "full"))] - GenericArgument::Const(_) => unreachable!(), - #[cfg(feature = "full")] - GenericArgument::Const(ref eb) => eb.to_tokens(tokens), + GenericArgument::Const(ref e) => match e.node { + ExprKind::Lit(_) => e.to_tokens(tokens), + + // NOTE: We should probably support parsing blocks with only + // expressions in them without the full feature for const + // generics. + #[cfg(feature = "full")] + ExprKind::Block(_) => e.to_tokens(tokens), + + // ERROR CORRECTION: Add braces to make sure that the + // generated code is valid. + _ => tokens::Brace::default().surround(tokens, |tokens| { + e.to_tokens(tokens); + }), + } } } }