Skip to content

Commit a3f1196

Browse files
committed
Auto merge of rust-lang#17962 - ChayimFriedman2:update-rtn, r=Veykril
fix: Fix Return Type Syntax to include `..` (i.e. `method(..)` and not `method()`) as specified in the RFC Fixes rust-lang#17952.
2 parents e0b1719 + 326a1c6 commit a3f1196

File tree

12 files changed

+183
-16
lines changed

12 files changed

+183
-16
lines changed

src/tools/rust-analyzer/crates/parser/src/grammar/generic_args.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,18 @@ fn generic_arg(p: &mut Parser<'_>) -> bool {
102102
IDENT if p.nth_at(1, T!['(']) => {
103103
let m = p.start();
104104
name_ref(p);
105-
params::param_list_fn_trait(p);
106-
if p.at(T![:]) && !p.at(T![::]) {
107-
// test associated_return_type_bounds
108-
// fn foo<T: Foo<foo(): Send, bar(i32): Send, baz(i32, i32): Send>>() {}
105+
if p.nth_at(1, T![..]) {
106+
let rtn = p.start();
107+
p.bump(T!['(']);
108+
p.bump(T![..]);
109+
p.expect(T![')']);
110+
rtn.complete(p, RETURN_TYPE_SYNTAX);
111+
// test return_type_syntax_assoc_type_bound
112+
// fn foo<T: Trait<method(..): Send>>() {}
109113
generic_params::bounds(p);
110114
m.complete(p, ASSOC_TYPE_ARG);
111115
} else {
116+
params::param_list_fn_trait(p);
112117
// test bare_dyn_types_with_paren_as_generic_args
113118
// type A = S<Fn(i32)>;
114119
// type A = S<Fn(i32) + Send>;

src/tools/rust-analyzer/crates/parser/src/grammar/generic_params.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ fn lifetime_bounds(p: &mut Parser<'_>) {
119119
// test type_param_bounds
120120
// struct S<T: 'a + ?Sized + (Copy) + ~const Drop>;
121121
pub(super) fn bounds(p: &mut Parser<'_>) {
122-
assert!(p.at(T![:]));
123-
p.bump(T![:]);
122+
p.expect(T![:]);
124123
bounds_without_colon(p);
125124
}
126125

src/tools/rust-analyzer/crates/parser/src/grammar/paths.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,24 @@ fn opt_path_type_args(p: &mut Parser<'_>, mode: Mode) {
140140
if p.at(T![::]) && p.nth_at(2, T!['(']) {
141141
p.bump(T![::]);
142142
}
143-
// test path_fn_trait_args
144-
// type F = Box<Fn(i32) -> ()>;
145143
if p.at(T!['(']) {
146-
params::param_list_fn_trait(p);
147-
opt_ret_type(p);
144+
if p.nth_at(1, T![..]) {
145+
// test return_type_syntax_in_path
146+
// fn foo<T>()
147+
// where
148+
// T::method(..): Send,
149+
// {}
150+
let rtn = p.start();
151+
p.bump(T!['(']);
152+
p.bump(T![..]);
153+
p.expect(T![')']);
154+
rtn.complete(p, RETURN_TYPE_SYNTAX);
155+
} else {
156+
// test path_fn_trait_args
157+
// type F = Box<Fn(i32) -> ()>;
158+
params::param_list_fn_trait(p);
159+
opt_ret_type(p);
160+
}
148161
} else {
149162
generic_args::opt_generic_arg_list(p, false);
150163
}

src/tools/rust-analyzer/crates/parser/src/syntax_kind/generated.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ pub enum SyntaxKind {
253253
RENAME,
254254
REST_PAT,
255255
RETURN_EXPR,
256+
RETURN_TYPE_SYNTAX,
256257
RET_TYPE,
257258
SELF_PARAM,
258259
SLICE_PAT,

src/tools/rust-analyzer/crates/parser/test_data/generated/runner.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ mod ok {
3737
#[test]
3838
fn assoc_type_eq() { run_and_expect_no_errors("test_data/parser/inline/ok/assoc_type_eq.rs"); }
3939
#[test]
40-
fn associated_return_type_bounds() {
41-
run_and_expect_no_errors("test_data/parser/inline/ok/associated_return_type_bounds.rs");
42-
}
43-
#[test]
4440
fn associated_type_bounds() {
4541
run_and_expect_no_errors("test_data/parser/inline/ok/associated_type_bounds.rs");
4642
}
@@ -519,6 +515,16 @@ mod ok {
519515
#[test]
520516
fn return_expr() { run_and_expect_no_errors("test_data/parser/inline/ok/return_expr.rs"); }
521517
#[test]
518+
fn return_type_syntax_assoc_type_bound() {
519+
run_and_expect_no_errors(
520+
"test_data/parser/inline/ok/return_type_syntax_assoc_type_bound.rs",
521+
);
522+
}
523+
#[test]
524+
fn return_type_syntax_in_path() {
525+
run_and_expect_no_errors("test_data/parser/inline/ok/return_type_syntax_in_path.rs");
526+
}
527+
#[test]
522528
fn self_param() { run_and_expect_no_errors("test_data/parser/inline/ok/self_param.rs"); }
523529
#[test]
524530
fn self_param_outer_attr() {

src/tools/rust-analyzer/crates/parser/test_data/parser/inline/ok/associated_return_type_bounds.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
SOURCE_FILE
2+
FN
3+
FN_KW "fn"
4+
WHITESPACE " "
5+
NAME
6+
IDENT "foo"
7+
GENERIC_PARAM_LIST
8+
L_ANGLE "<"
9+
TYPE_PARAM
10+
NAME
11+
IDENT "T"
12+
COLON ":"
13+
WHITESPACE " "
14+
TYPE_BOUND_LIST
15+
TYPE_BOUND
16+
PATH_TYPE
17+
PATH
18+
PATH_SEGMENT
19+
NAME_REF
20+
IDENT "Trait"
21+
GENERIC_ARG_LIST
22+
L_ANGLE "<"
23+
ASSOC_TYPE_ARG
24+
NAME_REF
25+
IDENT "method"
26+
RETURN_TYPE_SYNTAX
27+
L_PAREN "("
28+
DOT2 ".."
29+
R_PAREN ")"
30+
COLON ":"
31+
WHITESPACE " "
32+
TYPE_BOUND_LIST
33+
TYPE_BOUND
34+
PATH_TYPE
35+
PATH
36+
PATH_SEGMENT
37+
NAME_REF
38+
IDENT "Send"
39+
R_ANGLE ">"
40+
R_ANGLE ">"
41+
PARAM_LIST
42+
L_PAREN "("
43+
R_PAREN ")"
44+
WHITESPACE " "
45+
BLOCK_EXPR
46+
STMT_LIST
47+
L_CURLY "{"
48+
R_CURLY "}"
49+
WHITESPACE "\n"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn foo<T: Trait<method(..): Send>>() {}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
SOURCE_FILE
2+
FN
3+
FN_KW "fn"
4+
WHITESPACE " "
5+
NAME
6+
IDENT "foo"
7+
GENERIC_PARAM_LIST
8+
L_ANGLE "<"
9+
TYPE_PARAM
10+
NAME
11+
IDENT "T"
12+
R_ANGLE ">"
13+
PARAM_LIST
14+
L_PAREN "("
15+
R_PAREN ")"
16+
WHITESPACE "\n"
17+
WHERE_CLAUSE
18+
WHERE_KW "where"
19+
WHITESPACE "\n "
20+
WHERE_PRED
21+
PATH_TYPE
22+
PATH
23+
PATH
24+
PATH_SEGMENT
25+
NAME_REF
26+
IDENT "T"
27+
COLON2 "::"
28+
PATH_SEGMENT
29+
NAME_REF
30+
IDENT "method"
31+
RETURN_TYPE_SYNTAX
32+
L_PAREN "("
33+
DOT2 ".."
34+
R_PAREN ")"
35+
COLON ":"
36+
WHITESPACE " "
37+
TYPE_BOUND_LIST
38+
TYPE_BOUND
39+
PATH_TYPE
40+
PATH
41+
PATH_SEGMENT
42+
NAME_REF
43+
IDENT "Send"
44+
COMMA ","
45+
WHITESPACE "\n"
46+
BLOCK_EXPR
47+
STMT_LIST
48+
L_CURLY "{"
49+
R_CURLY "}"
50+
WHITESPACE "\n"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn foo<T>()
2+
where
3+
T::method(..): Send,
4+
{}

src/tools/rust-analyzer/crates/syntax/rust.ungram

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,12 @@ PathSegment =
3838
'::'? NameRef
3939
| NameRef GenericArgList?
4040
| NameRef ParamList RetType?
41+
| NameRef ReturnTypeSyntax
4142
| '<' Type ('as' PathType)? '>'
4243

44+
ReturnTypeSyntax =
45+
'(' '..' ')'
46+
4347

4448
//*************************//
4549
// Generics //
@@ -59,7 +63,7 @@ TypeArg =
5963

6064
AssocTypeArg =
6165
NameRef
62-
(GenericArgList | ParamList RetType?)?
66+
(GenericArgList | ParamList RetType? | ReturnTypeSyntax)?
6367
(':' TypeBoundList | ('=' Type | ConstArg))
6468

6569
LifetimeArg =

src/tools/rust-analyzer/crates/syntax/src/ast/generated/nodes.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ impl AssocTypeArg {
114114
#[inline]
115115
pub fn ret_type(&self) -> Option<RetType> { support::child(&self.syntax) }
116116
#[inline]
117+
pub fn return_type_syntax(&self) -> Option<ReturnTypeSyntax> { support::child(&self.syntax) }
118+
#[inline]
117119
pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) }
118120
#[inline]
119121
pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) }
@@ -1221,6 +1223,8 @@ impl PathSegment {
12211223
#[inline]
12221224
pub fn ret_type(&self) -> Option<RetType> { support::child(&self.syntax) }
12231225
#[inline]
1226+
pub fn return_type_syntax(&self) -> Option<ReturnTypeSyntax> { support::child(&self.syntax) }
1227+
#[inline]
12241228
pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) }
12251229
#[inline]
12261230
pub fn coloncolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![::]) }
@@ -1485,6 +1489,19 @@ impl ReturnExpr {
14851489
pub fn return_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![return]) }
14861490
}
14871491

1492+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1493+
pub struct ReturnTypeSyntax {
1494+
pub(crate) syntax: SyntaxNode,
1495+
}
1496+
impl ReturnTypeSyntax {
1497+
#[inline]
1498+
pub fn l_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['(']) }
1499+
#[inline]
1500+
pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) }
1501+
#[inline]
1502+
pub fn dotdot_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![..]) }
1503+
}
1504+
14881505
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
14891506
pub struct SelfParam {
14901507
pub(crate) syntax: SyntaxNode,
@@ -3697,6 +3714,20 @@ impl AstNode for ReturnExpr {
36973714
#[inline]
36983715
fn syntax(&self) -> &SyntaxNode { &self.syntax }
36993716
}
3717+
impl AstNode for ReturnTypeSyntax {
3718+
#[inline]
3719+
fn can_cast(kind: SyntaxKind) -> bool { kind == RETURN_TYPE_SYNTAX }
3720+
#[inline]
3721+
fn cast(syntax: SyntaxNode) -> Option<Self> {
3722+
if Self::can_cast(syntax.kind()) {
3723+
Some(Self { syntax })
3724+
} else {
3725+
None
3726+
}
3727+
}
3728+
#[inline]
3729+
fn syntax(&self) -> &SyntaxNode { &self.syntax }
3730+
}
37003731
impl AstNode for SelfParam {
37013732
#[inline]
37023733
fn can_cast(kind: SyntaxKind) -> bool { kind == SELF_PARAM }
@@ -6609,6 +6640,11 @@ impl std::fmt::Display for ReturnExpr {
66096640
std::fmt::Display::fmt(self.syntax(), f)
66106641
}
66116642
}
6643+
impl std::fmt::Display for ReturnTypeSyntax {
6644+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
6645+
std::fmt::Display::fmt(self.syntax(), f)
6646+
}
6647+
}
66126648
impl std::fmt::Display for SelfParam {
66136649
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66146650
std::fmt::Display::fmt(self.syntax(), f)

0 commit comments

Comments
 (0)