Skip to content

Commit 8b592db

Browse files
Add (..) syntax for RTN
1 parent 104aacb commit 8b592db

File tree

44 files changed

+355
-201
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+355
-201
lines changed

compiler/rustc_ast/src/ast.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -167,21 +167,21 @@ pub enum GenericArgs {
167167
AngleBracketed(AngleBracketedArgs),
168168
/// The `(A, B)` and `C` in `Foo(A, B) -> C`.
169169
Parenthesized(ParenthesizedArgs),
170+
/// Associated return type bounds, like `T: Trait<method(..): Send>`
171+
/// which applies the `Send` bound to the return-type of `method`.
172+
ReturnTypeNotation(Span),
170173
}
171174

172175
impl GenericArgs {
173176
pub fn is_angle_bracketed(&self) -> bool {
174177
matches!(self, AngleBracketed(..))
175178
}
176179

177-
pub fn is_parenthesized(&self) -> bool {
178-
matches!(self, Parenthesized(..))
179-
}
180-
181180
pub fn span(&self) -> Span {
182181
match self {
183182
AngleBracketed(data) => data.span,
184183
Parenthesized(data) => data.span,
184+
ReturnTypeNotation(span) => *span,
185185
}
186186
}
187187
}
@@ -235,15 +235,15 @@ impl AngleBracketedArg {
235235
}
236236
}
237237

238-
impl Into<Option<P<GenericArgs>>> for AngleBracketedArgs {
239-
fn into(self) -> Option<P<GenericArgs>> {
240-
Some(P(GenericArgs::AngleBracketed(self)))
238+
impl Into<P<GenericArgs>> for AngleBracketedArgs {
239+
fn into(self) -> P<GenericArgs> {
240+
P(GenericArgs::AngleBracketed(self))
241241
}
242242
}
243243

244-
impl Into<Option<P<GenericArgs>>> for ParenthesizedArgs {
245-
fn into(self) -> Option<P<GenericArgs>> {
246-
Some(P(GenericArgs::Parenthesized(self)))
244+
impl Into<P<GenericArgs>> for ParenthesizedArgs {
245+
fn into(self) -> P<GenericArgs> {
246+
P(GenericArgs::Parenthesized(self))
247247
}
248248
}
249249

compiler/rustc_ast/src/mut_visit.rs

+1
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@ pub fn noop_visit_generic_args<T: MutVisitor>(generic_args: &mut GenericArgs, vi
561561
match generic_args {
562562
GenericArgs::AngleBracketed(data) => vis.visit_angle_bracketed_parameter_data(data),
563563
GenericArgs::Parenthesized(data) => vis.visit_parenthesized_parameter_data(data),
564+
GenericArgs::ReturnTypeNotation(_span) => {}
564565
}
565566
}
566567

compiler/rustc_ast/src/visit.rs

+1
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ where
481481
walk_list!(visitor, visit_ty, &data.inputs);
482482
walk_fn_ret_ty(visitor, &data.output);
483483
}
484+
GenericArgs::ReturnTypeNotation(_span) => {}
484485
}
485486
}
486487

compiler/rustc_ast_lowering/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ ast_lowering_bad_return_type_notation_inputs =
144144
argument types not allowed with return type notation
145145
.suggestion = remove the input types
146146
147+
ast_lowering_bad_return_type_notation_needs_dots =
148+
return type notation arguments must be elided with `..`
149+
.suggestion = add `..`
150+
147151
ast_lowering_bad_return_type_notation_output =
148152
return type not allowed with return type notation
149153
.suggestion = remove the return type

compiler/rustc_ast_lowering/src/errors.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,13 @@ pub enum BadReturnTypeNotation {
353353
#[diag(ast_lowering_bad_return_type_notation_inputs)]
354354
Inputs {
355355
#[primary_span]
356-
#[suggestion(code = "()", applicability = "maybe-incorrect")]
356+
#[suggestion(code = "(..)", applicability = "maybe-incorrect")]
357+
span: Span,
358+
},
359+
#[diag(ast_lowering_bad_return_type_notation_needs_dots)]
360+
NeedsDots {
361+
#[primary_span]
362+
#[suggestion(code = "(..)", applicability = "maybe-incorrect")]
357363
span: Span,
358364
},
359365
#[diag(ast_lowering_bad_return_type_notation_output)]

compiler/rustc_ast_lowering/src/lib.rs

+55-30
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use rustc_middle::{
6666
span_bug,
6767
ty::{ResolverAstLowering, TyCtxt},
6868
};
69-
use rustc_session::parse::feature_err;
69+
use rustc_session::parse::{add_feature_diagnostics, feature_err};
7070
use rustc_span::hygiene::MacroKind;
7171
use rustc_span::source_map::DesugaringKind;
7272
use rustc_span::symbol::{kw, sym, Ident, Symbol};
@@ -987,33 +987,56 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
987987
GenericArgs::AngleBracketed(data) => {
988988
self.lower_angle_bracketed_parameter_data(data, ParamMode::Explicit, itctx).0
989989
}
990-
GenericArgs::Parenthesized(data) if self.tcx.features().return_type_notation => {
991-
if !data.inputs.is_empty() {
992-
self.tcx.sess.emit_err(errors::BadReturnTypeNotation::Inputs {
993-
span: data.inputs_span,
994-
});
995-
} else if let FnRetTy::Ty(ty) = &data.output {
996-
self.tcx.sess.emit_err(errors::BadReturnTypeNotation::Output {
997-
span: data.inputs_span.shrink_to_hi().to(ty.span),
998-
});
999-
}
1000-
GenericArgsCtor {
1001-
args: Default::default(),
1002-
bindings: &[],
1003-
parenthesized: true,
1004-
span: data.span,
1005-
}
1006-
}
990+
&GenericArgs::ReturnTypeNotation(span) => GenericArgsCtor {
991+
args: Default::default(),
992+
bindings: &[],
993+
parenthesized: hir::GenericArgsParentheses::ReturnTypeNotation,
994+
span,
995+
},
1007996
GenericArgs::Parenthesized(data) => {
1008-
self.emit_bad_parenthesized_trait_in_assoc_ty(data);
1009-
// FIXME(return_type_notation): we could issue a feature error
1010-
// if the parens are empty and there's no return type.
1011-
self.lower_angle_bracketed_parameter_data(
1012-
&data.as_angle_bracketed_args(),
1013-
ParamMode::Explicit,
1014-
itctx,
1015-
)
1016-
.0
997+
if let Some(start_char) = constraint.ident.as_str().chars().next()
998+
&& start_char.is_ascii_lowercase()
999+
{
1000+
let mut err = if !data.inputs.is_empty() {
1001+
self.tcx.sess.create_err(errors::BadReturnTypeNotation::Inputs {
1002+
span: data.inputs_span,
1003+
})
1004+
} else if let FnRetTy::Ty(ty) = &data.output {
1005+
self.tcx.sess.create_err(errors::BadReturnTypeNotation::Output {
1006+
span: data.inputs_span.shrink_to_hi().to(ty.span),
1007+
})
1008+
} else {
1009+
self.tcx.sess.create_err(errors::BadReturnTypeNotation::NeedsDots {
1010+
span: data.inputs_span,
1011+
})
1012+
};
1013+
if !self.tcx.features().return_type_notation
1014+
&& self.tcx.sess.is_nightly_build()
1015+
{
1016+
add_feature_diagnostics(
1017+
&mut err,
1018+
&self.tcx.sess.parse_sess,
1019+
sym::return_type_notation,
1020+
);
1021+
}
1022+
err.emit();
1023+
GenericArgsCtor {
1024+
args: Default::default(),
1025+
bindings: &[],
1026+
parenthesized: hir::GenericArgsParentheses::ReturnTypeNotation,
1027+
span: data.span,
1028+
}
1029+
} else {
1030+
self.emit_bad_parenthesized_trait_in_assoc_ty(data);
1031+
// FIXME(return_type_notation): we could issue a feature error
1032+
// if the parens are empty and there's no return type.
1033+
self.lower_angle_bracketed_parameter_data(
1034+
&data.as_angle_bracketed_args(),
1035+
ParamMode::Explicit,
1036+
itctx,
1037+
)
1038+
.0
1039+
}
10171040
}
10181041
};
10191042
gen_args_ctor.into_generic_args(self)
@@ -2094,7 +2117,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20942117
let future_args = self.arena.alloc(hir::GenericArgs {
20952118
args: &[],
20962119
bindings: arena_vec![self; self.output_ty_binding(span, output_ty)],
2097-
parenthesized: false,
2120+
parenthesized: hir::GenericArgsParentheses::No,
20982121
span_ext: DUMMY_SP,
20992122
});
21002123

@@ -2614,13 +2637,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
26142637
struct GenericArgsCtor<'hir> {
26152638
args: SmallVec<[hir::GenericArg<'hir>; 4]>,
26162639
bindings: &'hir [hir::TypeBinding<'hir>],
2617-
parenthesized: bool,
2640+
parenthesized: hir::GenericArgsParentheses,
26182641
span: Span,
26192642
}
26202643

26212644
impl<'hir> GenericArgsCtor<'hir> {
26222645
fn is_empty(&self) -> bool {
2623-
self.args.is_empty() && self.bindings.is_empty() && !self.parenthesized
2646+
self.args.is_empty()
2647+
&& self.bindings.is_empty()
2648+
&& self.parenthesized == hir::GenericArgsParentheses::No
26242649
}
26252650

26262651
fn into_generic_args(self, this: &LoweringContext<'_, 'hir>) -> &'hir hir::GenericArgs<'hir> {

compiler/rustc_ast_lowering/src/path.rs

+25-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_span::symbol::{kw, sym, Ident};
1313
use rustc_span::{BytePos, Span, DUMMY_SP};
1414

1515
use smallvec::{smallvec, SmallVec};
16+
use thin_vec::ThinVec;
1617

1718
impl<'a, 'hir> LoweringContext<'a, 'hir> {
1819
#[instrument(level = "trace", skip(self))]
@@ -218,13 +219,25 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
218219
)
219220
}
220221
},
222+
&GenericArgs::ReturnTypeNotation(span) => {
223+
self.tcx.sess.emit_err(GenericTypeWithParentheses { span, sub: None });
224+
(
225+
self.lower_angle_bracketed_parameter_data(
226+
&AngleBracketedArgs { span, args: ThinVec::default() },
227+
param_mode,
228+
itctx,
229+
)
230+
.0,
231+
false,
232+
)
233+
}
221234
}
222235
} else {
223236
(
224237
GenericArgsCtor {
225238
args: Default::default(),
226239
bindings: &[],
227-
parenthesized: false,
240+
parenthesized: hir::GenericArgsParentheses::No,
228241
span: path_span.shrink_to_hi(),
229242
},
230243
param_mode == ParamMode::Optional,
@@ -233,7 +246,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
233246

234247
let has_lifetimes =
235248
generic_args.args.iter().any(|arg| matches!(arg, GenericArg::Lifetime(_)));
236-
if !generic_args.parenthesized && !has_lifetimes {
249+
250+
// FIXME(return_type_notation): Is this correct? I think so.
251+
if generic_args.parenthesized != hir::GenericArgsParentheses::ParenSugar && !has_lifetimes {
237252
self.maybe_insert_elided_lifetimes_in_path(
238253
path_span,
239254
segment.id,
@@ -328,7 +343,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
328343
AngleBracketedArg::Constraint(c) => Some(self.lower_assoc_ty_constraint(c, itctx)),
329344
AngleBracketedArg::Arg(_) => None,
330345
}));
331-
let ctor = GenericArgsCtor { args, bindings, parenthesized: false, span: data.span };
346+
let ctor = GenericArgsCtor {
347+
args,
348+
bindings,
349+
parenthesized: hir::GenericArgsParentheses::No,
350+
span: data.span,
351+
};
332352
(ctor, !has_non_lt_args && param_mode == ParamMode::Optional)
333353
}
334354

@@ -376,7 +396,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
376396
GenericArgsCtor {
377397
args,
378398
bindings: arena_vec![self; binding],
379-
parenthesized: true,
399+
parenthesized: hir::GenericArgsParentheses::ParenSugar,
380400
span: data.inputs_span,
381401
},
382402
false,
@@ -396,7 +416,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
396416
let gen_args = self.arena.alloc(hir::GenericArgs {
397417
args,
398418
bindings,
399-
parenthesized: false,
419+
parenthesized: hir::GenericArgsParentheses::No,
400420
span_ext: DUMMY_SP,
401421
});
402422
hir::TypeBinding {

compiler/rustc_ast_passes/src/ast_validation.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10751075
self.with_impl_trait(None, |this| this.visit_ty(ty));
10761076
}
10771077
}
1078+
GenericArgs::ReturnTypeNotation(_span) => {}
10781079
}
10791080
}
10801081

@@ -1387,16 +1388,19 @@ fn deny_equality_constraints(
13871388
match &mut assoc_path.segments[len].args {
13881389
Some(args) => match args.deref_mut() {
13891390
GenericArgs::Parenthesized(_) => continue,
1391+
GenericArgs::ReturnTypeNotation(_span) => continue,
13901392
GenericArgs::AngleBracketed(args) => {
13911393
args.args.push(arg);
13921394
}
13931395
},
13941396
empty_args => {
1395-
*empty_args = AngleBracketedArgs {
1396-
span: ident.span,
1397-
args: thin_vec![arg],
1398-
}
1399-
.into();
1397+
*empty_args = Some(
1398+
AngleBracketedArgs {
1399+
span: ident.span,
1400+
args: thin_vec![arg],
1401+
}
1402+
.into(),
1403+
);
14001404
}
14011405
}
14021406
err.assoc = Some(errors::AssociatedSuggestion {

compiler/rustc_ast_passes/src/feature_gate.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -482,13 +482,20 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
482482

483483
fn visit_assoc_constraint(&mut self, constraint: &'a AssocConstraint) {
484484
if let AssocConstraintKind::Bound { .. } = constraint.kind {
485-
if constraint.gen_args.as_ref().map_or(false, |args| args.is_parenthesized()) {
486-
gate_feature_post!(
487-
&self,
488-
return_type_notation,
489-
constraint.span,
490-
"return type notation is unstable"
485+
if let Some(args) = constraint.gen_args.as_ref()
486+
&& matches!(
487+
args,
488+
ast::GenericArgs::ReturnTypeNotation(..) | ast::GenericArgs::Parenthesized(..)
491489
)
490+
{
491+
// RTN is gated elsewhere, and parenthesized args will turn into
492+
// another error.
493+
if matches!(args, ast::GenericArgs::Parenthesized(..)) {
494+
self.sess.delay_span_bug(
495+
constraint.span,
496+
"should have emitted a parenthesized generics error",
497+
);
498+
}
492499
} else {
493500
gate_feature_post!(
494501
&self,
@@ -586,6 +593,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) {
586593
gate_all!(yeet_expr, "`do yeet` expression is experimental");
587594
gate_all!(dyn_star, "`dyn*` trait objects are experimental");
588595
gate_all!(const_closures, "const closures are experimental");
596+
gate_all!(return_type_notation, "return type notation is experimental");
589597

590598
// All uses of `gate_all!` below this point were added in #65742,
591599
// and subsequently disabled (with the non-early gating readded).

compiler/rustc_ast_pretty/src/pprust/state.rs

+4
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,10 @@ impl<'a> PrintState<'a> for State<'a> {
936936
self.word(")");
937937
self.print_fn_ret_ty(&data.output);
938938
}
939+
940+
ast::GenericArgs::ReturnTypeNotation(_span) => {
941+
self.word("(..)");
942+
}
939943
}
940944
}
941945
}

compiler/rustc_expand/src/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl<'a> ExtCtxt<'a> {
3636
);
3737
let args = if !args.is_empty() {
3838
let args = args.into_iter().map(ast::AngleBracketedArg::Arg).collect();
39-
ast::AngleBracketedArgs { args, span }.into()
39+
Some(ast::AngleBracketedArgs { args, span }.into())
4040
} else {
4141
None
4242
};

0 commit comments

Comments
 (0)