Add (..) syntax for RTN

This commit is contained in:
Michael Goulet 2023-03-16 22:00:08 +00:00
parent 104aacb49f
commit 8b592db27a
44 changed files with 355 additions and 201 deletions

View file

@ -167,6 +167,9 @@ pub enum GenericArgs {
AngleBracketed(AngleBracketedArgs),
/// The `(A, B)` and `C` in `Foo(A, B) -> C`.
Parenthesized(ParenthesizedArgs),
/// Associated return type bounds, like `T: Trait<method(..): Send>`
/// which applies the `Send` bound to the return-type of `method`.
ReturnTypeNotation(Span),
}
impl GenericArgs {
@ -174,14 +177,11 @@ impl GenericArgs {
matches!(self, AngleBracketed(..))
}
pub fn is_parenthesized(&self) -> bool {
matches!(self, Parenthesized(..))
}
pub fn span(&self) -> Span {
match self {
AngleBracketed(data) => data.span,
Parenthesized(data) => data.span,
ReturnTypeNotation(span) => *span,
}
}
}
@ -235,15 +235,15 @@ impl AngleBracketedArg {
}
}
impl Into<Option<P<GenericArgs>>> for AngleBracketedArgs {
fn into(self) -> Option<P<GenericArgs>> {
Some(P(GenericArgs::AngleBracketed(self)))
impl Into<P<GenericArgs>> for AngleBracketedArgs {
fn into(self) -> P<GenericArgs> {
P(GenericArgs::AngleBracketed(self))
}
}
impl Into<Option<P<GenericArgs>>> for ParenthesizedArgs {
fn into(self) -> Option<P<GenericArgs>> {
Some(P(GenericArgs::Parenthesized(self)))
impl Into<P<GenericArgs>> for ParenthesizedArgs {
fn into(self) -> P<GenericArgs> {
P(GenericArgs::Parenthesized(self))
}
}

View file

@ -561,6 +561,7 @@ pub fn noop_visit_generic_args<T: MutVisitor>(generic_args: &mut GenericArgs, vi
match generic_args {
GenericArgs::AngleBracketed(data) => vis.visit_angle_bracketed_parameter_data(data),
GenericArgs::Parenthesized(data) => vis.visit_parenthesized_parameter_data(data),
GenericArgs::ReturnTypeNotation(_span) => {}
}
}

View file

@ -481,6 +481,7 @@ where
walk_list!(visitor, visit_ty, &data.inputs);
walk_fn_ret_ty(visitor, &data.output);
}
GenericArgs::ReturnTypeNotation(_span) => {}
}
}

View file

@ -144,6 +144,10 @@ ast_lowering_bad_return_type_notation_inputs =
argument types not allowed with return type notation
.suggestion = remove the input types
ast_lowering_bad_return_type_notation_needs_dots =
return type notation arguments must be elided with `..`
.suggestion = add `..`
ast_lowering_bad_return_type_notation_output =
return type not allowed with return type notation
.suggestion = remove the return type

View file

@ -353,7 +353,13 @@ pub enum BadReturnTypeNotation {
#[diag(ast_lowering_bad_return_type_notation_inputs)]
Inputs {
#[primary_span]
#[suggestion(code = "()", applicability = "maybe-incorrect")]
#[suggestion(code = "(..)", applicability = "maybe-incorrect")]
span: Span,
},
#[diag(ast_lowering_bad_return_type_notation_needs_dots)]
NeedsDots {
#[primary_span]
#[suggestion(code = "(..)", applicability = "maybe-incorrect")]
span: Span,
},
#[diag(ast_lowering_bad_return_type_notation_output)]

View file

@ -66,7 +66,7 @@ use rustc_middle::{
span_bug,
ty::{ResolverAstLowering, TyCtxt},
};
use rustc_session::parse::feature_err;
use rustc_session::parse::{add_feature_diagnostics, feature_err};
use rustc_span::hygiene::MacroKind;
use rustc_span::source_map::DesugaringKind;
use rustc_span::symbol::{kw, sym, Ident, Symbol};
@ -987,33 +987,56 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
GenericArgs::AngleBracketed(data) => {
self.lower_angle_bracketed_parameter_data(data, ParamMode::Explicit, itctx).0
}
GenericArgs::Parenthesized(data) if self.tcx.features().return_type_notation => {
if !data.inputs.is_empty() {
self.tcx.sess.emit_err(errors::BadReturnTypeNotation::Inputs {
span: data.inputs_span,
});
} else if let FnRetTy::Ty(ty) = &data.output {
self.tcx.sess.emit_err(errors::BadReturnTypeNotation::Output {
span: data.inputs_span.shrink_to_hi().to(ty.span),
});
}
GenericArgsCtor {
args: Default::default(),
bindings: &[],
parenthesized: true,
span: data.span,
}
}
&GenericArgs::ReturnTypeNotation(span) => GenericArgsCtor {
args: Default::default(),
bindings: &[],
parenthesized: hir::GenericArgsParentheses::ReturnTypeNotation,
span,
},
GenericArgs::Parenthesized(data) => {
self.emit_bad_parenthesized_trait_in_assoc_ty(data);
// FIXME(return_type_notation): we could issue a feature error
// if the parens are empty and there's no return type.
self.lower_angle_bracketed_parameter_data(
&data.as_angle_bracketed_args(),
ParamMode::Explicit,
itctx,
)
.0
if let Some(start_char) = constraint.ident.as_str().chars().next()
&& start_char.is_ascii_lowercase()
{
let mut err = if !data.inputs.is_empty() {
self.tcx.sess.create_err(errors::BadReturnTypeNotation::Inputs {
span: data.inputs_span,
})
} else if let FnRetTy::Ty(ty) = &data.output {
self.tcx.sess.create_err(errors::BadReturnTypeNotation::Output {
span: data.inputs_span.shrink_to_hi().to(ty.span),
})
} else {
self.tcx.sess.create_err(errors::BadReturnTypeNotation::NeedsDots {
span: data.inputs_span,
})
};
if !self.tcx.features().return_type_notation
&& self.tcx.sess.is_nightly_build()
{
add_feature_diagnostics(
&mut err,
&self.tcx.sess.parse_sess,
sym::return_type_notation,
);
}
err.emit();
GenericArgsCtor {
args: Default::default(),
bindings: &[],
parenthesized: hir::GenericArgsParentheses::ReturnTypeNotation,
span: data.span,
}
} else {
self.emit_bad_parenthesized_trait_in_assoc_ty(data);
// FIXME(return_type_notation): we could issue a feature error
// if the parens are empty and there's no return type.
self.lower_angle_bracketed_parameter_data(
&data.as_angle_bracketed_args(),
ParamMode::Explicit,
itctx,
)
.0
}
}
};
gen_args_ctor.into_generic_args(self)
@ -2094,7 +2117,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let future_args = self.arena.alloc(hir::GenericArgs {
args: &[],
bindings: arena_vec![self; self.output_ty_binding(span, output_ty)],
parenthesized: false,
parenthesized: hir::GenericArgsParentheses::No,
span_ext: DUMMY_SP,
});
@ -2614,13 +2637,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
struct GenericArgsCtor<'hir> {
args: SmallVec<[hir::GenericArg<'hir>; 4]>,
bindings: &'hir [hir::TypeBinding<'hir>],
parenthesized: bool,
parenthesized: hir::GenericArgsParentheses,
span: Span,
}
impl<'hir> GenericArgsCtor<'hir> {
fn is_empty(&self) -> bool {
self.args.is_empty() && self.bindings.is_empty() && !self.parenthesized
self.args.is_empty()
&& self.bindings.is_empty()
&& self.parenthesized == hir::GenericArgsParentheses::No
}
fn into_generic_args(self, this: &LoweringContext<'_, 'hir>) -> &'hir hir::GenericArgs<'hir> {

View file

@ -13,6 +13,7 @@ use rustc_span::symbol::{kw, sym, Ident};
use rustc_span::{BytePos, Span, DUMMY_SP};
use smallvec::{smallvec, SmallVec};
use thin_vec::ThinVec;
impl<'a, 'hir> LoweringContext<'a, 'hir> {
#[instrument(level = "trace", skip(self))]
@ -218,13 +219,25 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
)
}
},
&GenericArgs::ReturnTypeNotation(span) => {
self.tcx.sess.emit_err(GenericTypeWithParentheses { span, sub: None });
(
self.lower_angle_bracketed_parameter_data(
&AngleBracketedArgs { span, args: ThinVec::default() },
param_mode,
itctx,
)
.0,
false,
)
}
}
} else {
(
GenericArgsCtor {
args: Default::default(),
bindings: &[],
parenthesized: false,
parenthesized: hir::GenericArgsParentheses::No,
span: path_span.shrink_to_hi(),
},
param_mode == ParamMode::Optional,
@ -233,7 +246,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let has_lifetimes =
generic_args.args.iter().any(|arg| matches!(arg, GenericArg::Lifetime(_)));
if !generic_args.parenthesized && !has_lifetimes {
// FIXME(return_type_notation): Is this correct? I think so.
if generic_args.parenthesized != hir::GenericArgsParentheses::ParenSugar && !has_lifetimes {
self.maybe_insert_elided_lifetimes_in_path(
path_span,
segment.id,
@ -328,7 +343,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
AngleBracketedArg::Constraint(c) => Some(self.lower_assoc_ty_constraint(c, itctx)),
AngleBracketedArg::Arg(_) => None,
}));
let ctor = GenericArgsCtor { args, bindings, parenthesized: false, span: data.span };
let ctor = GenericArgsCtor {
args,
bindings,
parenthesized: hir::GenericArgsParentheses::No,
span: data.span,
};
(ctor, !has_non_lt_args && param_mode == ParamMode::Optional)
}
@ -376,7 +396,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
GenericArgsCtor {
args,
bindings: arena_vec![self; binding],
parenthesized: true,
parenthesized: hir::GenericArgsParentheses::ParenSugar,
span: data.inputs_span,
},
false,
@ -396,7 +416,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let gen_args = self.arena.alloc(hir::GenericArgs {
args,
bindings,
parenthesized: false,
parenthesized: hir::GenericArgsParentheses::No,
span_ext: DUMMY_SP,
});
hir::TypeBinding {

View file

@ -1075,6 +1075,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
self.with_impl_trait(None, |this| this.visit_ty(ty));
}
}
GenericArgs::ReturnTypeNotation(_span) => {}
}
}
@ -1387,16 +1388,19 @@ fn deny_equality_constraints(
match &mut assoc_path.segments[len].args {
Some(args) => match args.deref_mut() {
GenericArgs::Parenthesized(_) => continue,
GenericArgs::ReturnTypeNotation(_span) => continue,
GenericArgs::AngleBracketed(args) => {
args.args.push(arg);
}
},
empty_args => {
*empty_args = AngleBracketedArgs {
span: ident.span,
args: thin_vec![arg],
}
.into();
*empty_args = Some(
AngleBracketedArgs {
span: ident.span,
args: thin_vec![arg],
}
.into(),
);
}
}
err.assoc = Some(errors::AssociatedSuggestion {

View file

@ -482,13 +482,20 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
fn visit_assoc_constraint(&mut self, constraint: &'a AssocConstraint) {
if let AssocConstraintKind::Bound { .. } = constraint.kind {
if constraint.gen_args.as_ref().map_or(false, |args| args.is_parenthesized()) {
gate_feature_post!(
&self,
return_type_notation,
constraint.span,
"return type notation is unstable"
if let Some(args) = constraint.gen_args.as_ref()
&& matches!(
args,
ast::GenericArgs::ReturnTypeNotation(..) | ast::GenericArgs::Parenthesized(..)
)
{
// RTN is gated elsewhere, and parenthesized args will turn into
// another error.
if matches!(args, ast::GenericArgs::Parenthesized(..)) {
self.sess.delay_span_bug(
constraint.span,
"should have emitted a parenthesized generics error",
);
}
} else {
gate_feature_post!(
&self,
@ -586,6 +593,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) {
gate_all!(yeet_expr, "`do yeet` expression is experimental");
gate_all!(dyn_star, "`dyn*` trait objects are experimental");
gate_all!(const_closures, "const closures are experimental");
gate_all!(return_type_notation, "return type notation is experimental");
// All uses of `gate_all!` below this point were added in #65742,
// and subsequently disabled (with the non-early gating readded).

View file

@ -936,6 +936,10 @@ impl<'a> PrintState<'a> for State<'a> {
self.word(")");
self.print_fn_ret_ty(&data.output);
}
ast::GenericArgs::ReturnTypeNotation(_span) => {
self.word("(..)");
}
}
}
}

View file

@ -36,7 +36,7 @@ impl<'a> ExtCtxt<'a> {
);
let args = if !args.is_empty() {
let args = args.into_iter().map(ast::AngleBracketedArg::Arg).collect();
ast::AngleBracketedArgs { args, span }.into()
Some(ast::AngleBracketedArgs { args, span }.into())
} else {
None
};

View file

@ -328,7 +328,7 @@ pub struct GenericArgs<'hir> {
/// Were arguments written in parenthesized form `Fn(T) -> U`?
/// This is required mostly for pretty-printing and diagnostics,
/// but also for changing lifetime elision rules to be "function-like".
pub parenthesized: bool,
pub parenthesized: GenericArgsParentheses,
/// The span encompassing arguments and the surrounding brackets `<>` or `()`
/// Foo<A, B, AssocTy = D> Fn(T, U, V) -> W
/// ^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^
@ -340,11 +340,16 @@ pub struct GenericArgs<'hir> {
impl<'hir> GenericArgs<'hir> {
pub const fn none() -> Self {
Self { args: &[], bindings: &[], parenthesized: false, span_ext: DUMMY_SP }
Self {
args: &[],
bindings: &[],
parenthesized: GenericArgsParentheses::No,
span_ext: DUMMY_SP,
}
}
pub fn inputs(&self) -> &[Ty<'hir>] {
if self.parenthesized {
if self.parenthesized == GenericArgsParentheses::ParenSugar {
for arg in self.args {
match arg {
GenericArg::Lifetime(_) => {}
@ -417,6 +422,17 @@ impl<'hir> GenericArgs<'hir> {
}
}
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Hash, Debug)]
#[derive(HashStable_Generic)]
pub enum GenericArgsParentheses {
No,
/// Bounds for `feature(return_type_notation)`, like `T: Trait<method(..): Send>`,
/// where the args are explicitly elided with `..`
ReturnTypeNotation,
/// parenthesized function-family traits, like `T: Fn(u32) -> i32`
ParenSugar,
}
/// A modifier on a bound, currently this is only used for `?Sized`, where the
/// modifier is `Maybe`. Negative bounds should also be handled here.
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Hash, Debug)]

View file

@ -55,7 +55,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let trait_def = self.tcx().trait_def(trait_def_id);
if !trait_def.paren_sugar {
if trait_segment.args().parenthesized {
if trait_segment.args().parenthesized == hir::GenericArgsParentheses::ParenSugar {
// For now, require that parenthetical notation be used only with `Fn()` etc.
let mut err = feature_err(
&self.tcx().sess.parse_sess,
@ -71,7 +71,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let sess = self.tcx().sess;
if !trait_segment.args().parenthesized {
if trait_segment.args().parenthesized != hir::GenericArgsParentheses::ParenSugar {
// For now, require that parenthetical notation be used only with `Fn()` etc.
let mut err = feature_err(
&sess.parse_sess,
@ -607,11 +607,19 @@ pub fn prohibit_assoc_ty_binding(
span: Span,
segment: Option<(&hir::PathSegment<'_>, Span)>,
) {
tcx.sess.emit_err(AssocTypeBindingNotAllowed { span, fn_trait_expansion: if let Some((segment, span)) = segment && segment.args().parenthesized {
Some(ParenthesizedFnTraitExpansion { span, expanded_type: fn_trait_to_string(tcx, segment, false) })
} else {
None
}});
tcx.sess.emit_err(AssocTypeBindingNotAllowed {
span,
fn_trait_expansion: if let Some((segment, span)) = segment
&& segment.args().parenthesized == hir::GenericArgsParentheses::ParenSugar
{
Some(ParenthesizedFnTraitExpansion {
span,
expanded_type: fn_trait_to_string(tcx, segment, false),
})
} else {
None
},
});
}
pub(crate) fn fn_trait_to_string(

View file

@ -1087,7 +1087,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let tcx = self.tcx();
let return_type_notation =
binding.gen_args.parenthesized && tcx.features().return_type_notation;
binding.gen_args.parenthesized == hir::GenericArgsParentheses::ReturnTypeNotation;
let candidate = if return_type_notation {
if self.trait_defines_associated_item_named(

View file

@ -1461,7 +1461,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
depth: usize,
generic_args: &'tcx hir::GenericArgs<'tcx>,
) {
if generic_args.parenthesized {
if generic_args.parenthesized == hir::GenericArgsParentheses::ParenSugar {
self.visit_fn_like_elision(
generic_args.inputs(),
Some(generic_args.bindings[0].ty()),
@ -1653,7 +1653,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
// `for<'a> T::Trait<'a, x(): for<'b> Other<'b>>`
// this is going to expand to something like:
// `for<'a> for<'r, T> <T as Trait<'a>>::x::<'r, T>::{opaque#0}: for<'b> Other<'b>`.
if binding.gen_args.parenthesized {
if binding.gen_args.parenthesized == hir::GenericArgsParentheses::ReturnTypeNotation {
let bound_vars = if let Some(type_def_id) = type_def_id
&& self.tcx.def_kind(type_def_id) == DefKind::Trait
// FIXME(return_type_notation): We could bound supertrait methods.

View file

@ -565,7 +565,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
/// type Map = HashMap<String>;
/// ```
fn suggest_adding_args(&self, err: &mut Diagnostic) {
if self.gen_args.parenthesized {
if self.gen_args.parenthesized != hir::GenericArgsParentheses::No {
return;
}
@ -962,7 +962,11 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
let msg = format!(
"remove these {}generics",
if self.gen_args.parenthesized { "parenthetical " } else { "" },
if self.gen_args.parenthesized == hir::GenericArgsParentheses::ParenSugar {
"parenthetical "
} else {
""
},
);
err.span_suggestion(span, &msg, "", Applicability::MaybeIncorrect);

View file

@ -1652,61 +1652,65 @@ impl<'a> State<'a> {
generic_args: &hir::GenericArgs<'_>,
colons_before_params: bool,
) {
if generic_args.parenthesized {
self.word("(");
self.commasep(Inconsistent, generic_args.inputs(), |s, ty| s.print_type(ty));
self.word(")");
match generic_args.parenthesized {
hir::GenericArgsParentheses::No => {
let start = if colons_before_params { "::<" } else { "<" };
let empty = Cell::new(true);
let start_or_comma = |this: &mut Self| {
if empty.get() {
empty.set(false);
this.word(start)
} else {
this.word_space(",")
}
};
self.space_if_not_bol();
self.word_space("->");
self.print_type(generic_args.bindings[0].ty());
} else {
let start = if colons_before_params { "::<" } else { "<" };
let empty = Cell::new(true);
let start_or_comma = |this: &mut Self| {
if empty.get() {
empty.set(false);
this.word(start)
} else {
this.word_space(",")
}
};
let mut nonelided_generic_args: bool = false;
let elide_lifetimes = generic_args.args.iter().all(|arg| match arg {
GenericArg::Lifetime(lt) if lt.is_elided() => true,
GenericArg::Lifetime(_) => {
nonelided_generic_args = true;
false
}
_ => {
nonelided_generic_args = true;
true
}
});
let mut nonelided_generic_args: bool = false;
let elide_lifetimes = generic_args.args.iter().all(|arg| match arg {
GenericArg::Lifetime(lt) if lt.is_elided() => true,
GenericArg::Lifetime(_) => {
nonelided_generic_args = true;
false
if nonelided_generic_args {
start_or_comma(self);
self.commasep(Inconsistent, generic_args.args, |s, generic_arg| {
match generic_arg {
GenericArg::Lifetime(lt) if !elide_lifetimes => s.print_lifetime(lt),
GenericArg::Lifetime(_) => {}
GenericArg::Type(ty) => s.print_type(ty),
GenericArg::Const(ct) => s.print_anon_const(&ct.value),
GenericArg::Infer(_inf) => s.word("_"),
}
});
}
_ => {
nonelided_generic_args = true;
true
}
});
if nonelided_generic_args {
start_or_comma(self);
self.commasep(
Inconsistent,
generic_args.args,
|s, generic_arg| match generic_arg {
GenericArg::Lifetime(lt) if !elide_lifetimes => s.print_lifetime(lt),
GenericArg::Lifetime(_) => {}
GenericArg::Type(ty) => s.print_type(ty),
GenericArg::Const(ct) => s.print_anon_const(&ct.value),
GenericArg::Infer(_inf) => s.word("_"),
},
);
for binding in generic_args.bindings {
start_or_comma(self);
self.print_type_binding(binding);
}
if !empty.get() {
self.word(">")
}
}
hir::GenericArgsParentheses::ParenSugar => {
self.word("(");
self.commasep(Inconsistent, generic_args.inputs(), |s, ty| s.print_type(ty));
self.word(")");
for binding in generic_args.bindings {
start_or_comma(self);
self.print_type_binding(binding);
self.space_if_not_bol();
self.word_space("->");
self.print_type(generic_args.bindings[0].ty());
}
if !empty.get() {
self.word(">")
hir::GenericArgsParentheses::ReturnTypeNotation => {
self.word("(..)");
}
}
}

View file

@ -734,3 +734,7 @@ parse_unknown_start_of_token = unknown start of token: {$escaped}
parse_box_syntax_removed = `box_syntax` has been removed
.suggestion = use `Box::new()` instead
parse_bad_return_type_notation_output =
return type not allowed with return type notation
.suggestion = remove the return type

View file

@ -2316,3 +2316,11 @@ pub struct BoxSyntaxRemoved<'a> {
pub span: Span,
pub code: &'a str,
}
#[derive(Diagnostic)]
#[diag(parse_bad_return_type_notation_output)]
pub(crate) struct BadReturnTypeNotationOutput {
#[primary_span]
#[suggestion(code = "", applicability = "maybe-incorrect")]
pub span: Span,
}

View file

@ -989,8 +989,7 @@ impl<'a> Parser<'a> {
}
if self.token.kind == token::OpenDelim(Delimiter::Parenthesis) {
// Recover from bad turbofish: `foo.collect::Vec<_>()`.
let args = AngleBracketedArgs { args, span }.into();
segment.args = args;
segment.args = Some(AngleBracketedArgs { args, span }.into());
self.sess.emit_err(GenericParamsWithoutAngleBrackets {
span,

View file

@ -1,6 +1,6 @@
use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
use super::{Parser, Restrictions, TokenType};
use crate::maybe_whole;
use crate::{errors, maybe_whole};
use rustc_ast::ptr::P;
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
use rustc_ast::{
@ -290,6 +290,25 @@ impl<'a> Parser<'a> {
})?;
let span = lo.to(self.prev_token.span);
AngleBracketedArgs { args, span }.into()
} else if self.token.kind == token::OpenDelim(Delimiter::Parenthesis)
// FIXME(return_type_notation): Could also recover `...` here.
&& self.look_ahead(1, |tok| tok.kind == token::DotDot)
{
let lo = self.token.span;
self.bump();
self.bump();
self.expect(&token::CloseDelim(Delimiter::Parenthesis))?;
let span = lo.to(self.prev_token.span);
self.sess.gated_spans.gate(sym::return_type_notation, span);
if self.eat_noexpect(&token::RArrow) {
let lo = self.prev_token.span;
let ty = self.parse_ty()?;
self.sess
.emit_err(errors::BadReturnTypeNotationOutput { span: lo.to(ty.span) });
}
P(GenericArgs::ReturnTypeNotation(span))
} else {
// `(T, U) -> R`
let (inputs, _) = self.parse_paren_comma_seq(|p| p.parse_ty())?;
@ -300,7 +319,7 @@ impl<'a> Parser<'a> {
ParenthesizedArgs { span, inputs, inputs_span, output }.into()
};
PathSegment { ident, args, id: ast::DUMMY_NODE_ID }
PathSegment { ident, args: Some(args), id: ast::DUMMY_NODE_ID }
} else {
// Generic arguments are not found.
PathSegment::from_ident(ident)
@ -550,8 +569,10 @@ impl<'a> Parser<'a> {
// Gate associated type bounds, e.g., `Iterator<Item: Ord>`.
if let AssocConstraintKind::Bound { .. } = kind {
if gen_args.as_ref().map_or(false, |args| args.is_parenthesized()) {
self.sess.gated_spans.gate(sym::return_type_notation, span);
if gen_args.as_ref().map_or(false, |args| {
matches!(args, GenericArgs::ReturnTypeNotation(..))
}) {
// This is already gated in `parse_path_segment`
} else {
self.sess.gated_spans.gate(sym::associated_type_bounds, span);
}

View file

@ -1059,8 +1059,11 @@ impl<'a> Parser<'a> {
output,
}
.into();
*fn_path_segment =
ast::PathSegment { ident: fn_path_segment.ident, args, id: ast::DUMMY_NODE_ID };
*fn_path_segment = ast::PathSegment {
ident: fn_path_segment.ident,
args: Some(args),
id: ast::DUMMY_NODE_ID,
};
// Convert parsed `<'a>` in `Fn<'a>` into `for<'a>`.
let mut generic_params = lifetimes

View file

@ -666,7 +666,7 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
fn visit_generic_args(&mut self, g: &'v ast::GenericArgs) {
record_variants!(
(self, g, g, Id::None, ast, GenericArgs, GenericArgs),
[AngleBracketed, Parenthesized]
[AngleBracketed, Parenthesized, ReturnTypeNotation]
);
ast_visit::walk_generic_args(self, g)
}

View file

@ -1110,6 +1110,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
}
}
}
GenericArgs::ReturnTypeNotation(_span) => {}
}
}
}

View file

@ -312,6 +312,7 @@ impl<'a> From<&'a ast::PathSegment> for Segment {
(args.span, found_lifetimes)
}
GenericArgs::Parenthesized(args) => (args.span, true),
GenericArgs::ReturnTypeNotation(span) => (*span, false),
}
} else {
(DUMMY_SP, false)