X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=compiler%2Frustc_parse%2Fsrc%2Fparser%2Fdiagnostics.rs;h=b844e96d39c54132877b2e69bbb87d818648afe1;hb=cfc0bd12581651e5d0f51d0d4c2d8306cc13cb51;hp=25beed1ecf9c776413e7580c683ebe0197b6406a;hpb=6f34c0d2a2288dd0167f0710a50370a259ac9a2b;p=rust.git diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 25beed1ecf9..b844e96d39c 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -1,5 +1,5 @@ use super::pat::Expected; -use super::ty::AllowPlus; +use super::ty::{AllowPlus, IsAsCast}; use super::{ BlockMode, Parser, PathStyle, RecoverColon, RecoverComma, Restrictions, SemiColonMode, SeqSep, TokenExpectType, TokenType, @@ -1032,6 +1032,34 @@ pub(super) fn maybe_report_ambiguous_plus( } } + /// Swift lets users write `Ty?` to mean `Option`. Parse the construct and recover from it. + pub(super) fn maybe_recover_from_question_mark( + &mut self, + ty: P, + is_as_cast: IsAsCast, + ) -> P { + if let IsAsCast::Yes = is_as_cast { + return ty; + } + if self.token == token::Question { + self.bump(); + self.struct_span_err(self.prev_token.span, "invalid `?` in type") + .span_label(self.prev_token.span, "`?` is only allowed on expressions, not types") + .multipart_suggestion( + "if you meant to express that the type might not contain a value, use the `Option` wrapper type", + vec![ + (ty.span.shrink_to_lo(), "Option<".to_string()), + (self.prev_token.span, ">".to_string()), + ], + Applicability::MachineApplicable, + ) + .emit(); + self.mk_ty(ty.span.to(self.prev_token.span), TyKind::Err) + } else { + ty + } + } + pub(super) fn maybe_recover_from_bad_type_plus( &mut self, allow_plus: AllowPlus, @@ -2236,7 +2264,7 @@ pub(super) fn incorrect_move_async_order_found( err.span_suggestion( seq_span, "...or a vertical bar to match on multiple alternatives", - seq_snippet.replace(",", " |"), + seq_snippet.replace(',', " |"), Applicability::MachineApplicable, ); }