mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 13:03:31 +00:00
Merge #11683
11683: fix: Stop wrapping ConstParam's default values in ConstArg r=Veykril a=steven-joruk I came across this problem while implementing the assist for inlining type aliases. This was causing `ConstParam::default_val` to always return `None` for block expressions. The `const_arg_path` test was actually testing const params so I've updated that. The only code that uses `default_val` right now is the `extract_function` assist. I couldn't figure out how to hit the affected code path, if someone can give me hint I'll add a test. This test in my WIP branch fails without this: ```rust #[test] fn param_expression() { check_assist( inline_type_alias, r#" type A<const N: usize = { 1 }> = [u32; N]; fn main() { let a: $0A; } "#, r#" type A<const N: usize = { 1 }> = [u32; N]; fn main() { let a: [u32; { 1 }]; } "#, ); } ``` Co-authored-by: Steven Joruk <steven@joruk.com>
This commit is contained in:
commit
bbe61565bb
9 changed files with 69 additions and 29 deletions
|
@ -72,28 +72,24 @@ fn lifetime_arg(p: &mut Parser) {
|
||||||
m.complete(p, LIFETIME_ARG);
|
m.complete(p, LIFETIME_ARG);
|
||||||
}
|
}
|
||||||
|
|
||||||
// test const_arg
|
pub(super) fn const_arg_expr(p: &mut Parser) {
|
||||||
// type T = S<92>;
|
// The tests in here are really for `const_arg`, which wraps the content
|
||||||
pub(super) fn const_arg(p: &mut Parser) {
|
// CONST_ARG.
|
||||||
let m = p.start();
|
|
||||||
match p.current() {
|
match p.current() {
|
||||||
// test const_arg_block
|
// test const_arg_block
|
||||||
// type T = S<{90 + 2}>;
|
// type T = S<{90 + 2}>;
|
||||||
T!['{'] => {
|
T!['{'] => {
|
||||||
expressions::block_expr(p);
|
expressions::block_expr(p);
|
||||||
m.complete(p, CONST_ARG);
|
|
||||||
}
|
}
|
||||||
// test const_arg_literal
|
// test const_arg_literal
|
||||||
// type T = S<"hello", 0xdeadbeef>;
|
// type T = S<"hello", 0xdeadbeef>;
|
||||||
k if k.is_literal() => {
|
k if k.is_literal() => {
|
||||||
expressions::literal(p);
|
expressions::literal(p);
|
||||||
m.complete(p, CONST_ARG);
|
|
||||||
}
|
}
|
||||||
// test const_arg_bool_literal
|
// test const_arg_bool_literal
|
||||||
// type T = S<true>;
|
// type T = S<true>;
|
||||||
T![true] | T![false] => {
|
T![true] | T![false] => {
|
||||||
expressions::literal(p);
|
expressions::literal(p);
|
||||||
m.complete(p, CONST_ARG);
|
|
||||||
}
|
}
|
||||||
// test const_arg_negative_number
|
// test const_arg_negative_number
|
||||||
// type T = S<-92>;
|
// type T = S<-92>;
|
||||||
|
@ -102,19 +98,24 @@ pub(super) fn const_arg(p: &mut Parser) {
|
||||||
p.bump(T![-]);
|
p.bump(T![-]);
|
||||||
expressions::literal(p);
|
expressions::literal(p);
|
||||||
lm.complete(p, PREFIX_EXPR);
|
lm.complete(p, PREFIX_EXPR);
|
||||||
m.complete(p, CONST_ARG);
|
|
||||||
}
|
}
|
||||||
// test const_arg_path
|
|
||||||
// struct S<const N: u32 = u32::MAX>;
|
|
||||||
_ => {
|
_ => {
|
||||||
|
// This shouldn't be hit by `const_arg`
|
||||||
let lm = p.start();
|
let lm = p.start();
|
||||||
paths::use_path(p);
|
paths::use_path(p);
|
||||||
lm.complete(p, PATH_EXPR);
|
lm.complete(p, PATH_EXPR);
|
||||||
m.complete(p, CONST_ARG);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// test const_arg
|
||||||
|
// type T = S<92>;
|
||||||
|
pub(super) fn const_arg(p: &mut Parser) {
|
||||||
|
let m = p.start();
|
||||||
|
const_arg_expr(p);
|
||||||
|
m.complete(p, CONST_ARG);
|
||||||
|
}
|
||||||
|
|
||||||
fn type_arg(p: &mut Parser) {
|
fn type_arg(p: &mut Parser) {
|
||||||
let m = p.start();
|
let m = p.start();
|
||||||
types::type_(p);
|
types::type_(p);
|
||||||
|
|
|
@ -79,10 +79,16 @@ fn const_param(p: &mut Parser, m: Marker) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.at(T![=]) {
|
if p.at(T![=]) {
|
||||||
// test const_param_defaults
|
// test const_param_default_literal
|
||||||
// struct A<const N: i32 = -1>;
|
// struct A<const N: i32 = -1>;
|
||||||
p.bump(T![=]);
|
p.bump(T![=]);
|
||||||
generic_args::const_arg(p);
|
|
||||||
|
// test const_param_default_expression
|
||||||
|
// struct A<const N: i32 = { 1 }>;
|
||||||
|
|
||||||
|
// test const_param_default_path
|
||||||
|
// struct A<const N: i32 = i32::MAX>;
|
||||||
|
generic_args::const_arg_expr(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
m.complete(p, CONST_PARAM);
|
m.complete(p, CONST_PARAM);
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
struct S<const N: u32 = u32::MAX>;
|
|
|
@ -3,7 +3,7 @@ SOURCE_FILE
|
||||||
STRUCT_KW "struct"
|
STRUCT_KW "struct"
|
||||||
WHITESPACE " "
|
WHITESPACE " "
|
||||||
NAME
|
NAME
|
||||||
IDENT "S"
|
IDENT "A"
|
||||||
GENERIC_PARAM_LIST
|
GENERIC_PARAM_LIST
|
||||||
L_ANGLE "<"
|
L_ANGLE "<"
|
||||||
CONST_PARAM
|
CONST_PARAM
|
||||||
|
@ -17,21 +17,20 @@ SOURCE_FILE
|
||||||
PATH
|
PATH
|
||||||
PATH_SEGMENT
|
PATH_SEGMENT
|
||||||
NAME_REF
|
NAME_REF
|
||||||
IDENT "u32"
|
IDENT "i32"
|
||||||
WHITESPACE " "
|
WHITESPACE " "
|
||||||
EQ "="
|
EQ "="
|
||||||
WHITESPACE " "
|
WHITESPACE " "
|
||||||
CONST_ARG
|
PATH_EXPR
|
||||||
PATH_EXPR
|
PATH
|
||||||
PATH
|
PATH
|
||||||
PATH
|
|
||||||
PATH_SEGMENT
|
|
||||||
NAME_REF
|
|
||||||
IDENT "u32"
|
|
||||||
COLON2 "::"
|
|
||||||
PATH_SEGMENT
|
PATH_SEGMENT
|
||||||
NAME_REF
|
NAME_REF
|
||||||
IDENT "MAX"
|
IDENT "i32"
|
||||||
|
COLON2 "::"
|
||||||
|
PATH_SEGMENT
|
||||||
|
NAME_REF
|
||||||
|
IDENT "MAX"
|
||||||
R_ANGLE ">"
|
R_ANGLE ">"
|
||||||
SEMICOLON ";"
|
SEMICOLON ";"
|
||||||
WHITESPACE "\n"
|
WHITESPACE "\n"
|
|
@ -0,0 +1 @@
|
||||||
|
struct A<const N: i32 = i32::MAX>;
|
|
@ -0,0 +1,34 @@
|
||||||
|
SOURCE_FILE
|
||||||
|
STRUCT
|
||||||
|
STRUCT_KW "struct"
|
||||||
|
WHITESPACE " "
|
||||||
|
NAME
|
||||||
|
IDENT "A"
|
||||||
|
GENERIC_PARAM_LIST
|
||||||
|
L_ANGLE "<"
|
||||||
|
CONST_PARAM
|
||||||
|
CONST_KW "const"
|
||||||
|
WHITESPACE " "
|
||||||
|
NAME
|
||||||
|
IDENT "N"
|
||||||
|
COLON ":"
|
||||||
|
WHITESPACE " "
|
||||||
|
PATH_TYPE
|
||||||
|
PATH
|
||||||
|
PATH_SEGMENT
|
||||||
|
NAME_REF
|
||||||
|
IDENT "i32"
|
||||||
|
WHITESPACE " "
|
||||||
|
EQ "="
|
||||||
|
WHITESPACE " "
|
||||||
|
BLOCK_EXPR
|
||||||
|
STMT_LIST
|
||||||
|
L_CURLY "{"
|
||||||
|
WHITESPACE " "
|
||||||
|
LITERAL
|
||||||
|
INT_NUMBER "1"
|
||||||
|
WHITESPACE " "
|
||||||
|
R_CURLY "}"
|
||||||
|
R_ANGLE ">"
|
||||||
|
SEMICOLON ";"
|
||||||
|
WHITESPACE "\n"
|
|
@ -0,0 +1 @@
|
||||||
|
struct A<const N: i32 = { 1 }>;
|
|
@ -21,11 +21,10 @@ SOURCE_FILE
|
||||||
WHITESPACE " "
|
WHITESPACE " "
|
||||||
EQ "="
|
EQ "="
|
||||||
WHITESPACE " "
|
WHITESPACE " "
|
||||||
CONST_ARG
|
PREFIX_EXPR
|
||||||
PREFIX_EXPR
|
MINUS "-"
|
||||||
MINUS "-"
|
LITERAL
|
||||||
LITERAL
|
INT_NUMBER "1"
|
||||||
INT_NUMBER "1"
|
|
||||||
R_ANGLE ">"
|
R_ANGLE ">"
|
||||||
SEMICOLON ";"
|
SEMICOLON ";"
|
||||||
WHITESPACE "\n"
|
WHITESPACE "\n"
|
Loading…
Reference in a new issue