From defcc44238d9d79b7bcf5dfae2ec33001f568dd0 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Fri, 15 Mar 2024 11:36:21 +0000 Subject: [PATCH 1/2] Make `unexpected` always "return" `PResult<()>` & add `unexpected_any` This prevents breakage when `?` no longer skews inference. --- compiler/rustc_builtin_macros/src/asm.rs | 2 +- compiler/rustc_builtin_macros/src/assert.rs | 2 +- compiler/rustc_parse/src/parser/attr.rs | 6 +++--- compiler/rustc_parse/src/parser/generics.rs | 2 +- compiler/rustc_parse/src/parser/item.rs | 12 ++++++------ compiler/rustc_parse/src/parser/mod.rs | 14 ++++++++++++-- compiler/rustc_parse/src/parser/path.rs | 2 +- 7 files changed, 25 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/asm.rs b/compiler/rustc_builtin_macros/src/asm.rs index 62c02817011fd..76805617c933b 100644 --- a/compiler/rustc_builtin_macros/src/asm.rs +++ b/compiler/rustc_builtin_macros/src/asm.rs @@ -189,7 +189,7 @@ pub fn parse_asm_args<'a>( args.templates.push(template); continue; } else { - return p.unexpected(); + p.unexpected_any()? }; allow_templates = false; diff --git a/compiler/rustc_builtin_macros/src/assert.rs b/compiler/rustc_builtin_macros/src/assert.rs index 35a0857fe51ce..5905bdd710849 100644 --- a/compiler/rustc_builtin_macros/src/assert.rs +++ b/compiler/rustc_builtin_macros/src/assert.rs @@ -151,7 +151,7 @@ fn parse_assert<'a>(cx: &mut ExtCtxt<'a>, sp: Span, stream: TokenStream) -> PRes }; if parser.token != token::Eof { - return parser.unexpected(); + parser.unexpected()?; } Ok(Assert { cond_expr, custom_message }) diff --git a/compiler/rustc_parse/src/parser/attr.rs b/compiler/rustc_parse/src/parser/attr.rs index eb9a10f4bdab1..d08c50b5b0628 100644 --- a/compiler/rustc_parse/src/parser/attr.rs +++ b/compiler/rustc_parse/src/parser/attr.rs @@ -380,12 +380,12 @@ impl<'a> Parser<'a> { }; if let Some(item) = nt_meta { - return match item.meta(item.path.span) { + match item.meta(item.path.span) { Some(meta) => { self.bump(); - Ok(meta) + return Ok(meta); } - None => self.unexpected(), + None => self.unexpected()?, }; } diff --git a/compiler/rustc_parse/src/parser/generics.rs b/compiler/rustc_parse/src/parser/generics.rs index 263b2a9064356..fde16ac957dfe 100644 --- a/compiler/rustc_parse/src/parser/generics.rs +++ b/compiler/rustc_parse/src/parser/generics.rs @@ -481,7 +481,7 @@ impl<'a> Parser<'a> { })) } else { self.maybe_recover_bounds_doubled_colon(&ty)?; - self.unexpected() + self.unexpected_any() } } diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 79492fe62ed97..18d210eacfa61 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -1514,7 +1514,7 @@ impl<'a> Parser<'a> { let ident = this.parse_field_ident("enum", vlo)?; if this.token == token::Not { - if let Err(err) = this.unexpected::<()>() { + if let Err(err) = this.unexpected() { err.with_note(fluent::parse_macro_expands_to_enum_variant).emit(); } @@ -1937,7 +1937,7 @@ impl<'a> Parser<'a> { ) -> PResult<'a, FieldDef> { let name = self.parse_field_ident(adt_ty, lo)?; if self.token.kind == token::Not { - if let Err(mut err) = self.unexpected::() { + if let Err(mut err) = self.unexpected() { // Encounter the macro invocation err.subdiagnostic(self.dcx(), MacroExpandsToAdtField { adt_ty }); return Err(err); @@ -2067,7 +2067,7 @@ impl<'a> Parser<'a> { let params = self.parse_token_tree(); // `MacParams` let pspan = params.span(); if !self.check(&token::OpenDelim(Delimiter::Brace)) { - return self.unexpected(); + self.unexpected()?; } let body = self.parse_token_tree(); // `MacBody` // Convert `MacParams MacBody` into `{ MacParams => MacBody }`. @@ -2077,7 +2077,7 @@ impl<'a> Parser<'a> { let dspan = DelimSpan::from_pair(pspan.shrink_to_lo(), bspan.shrink_to_hi()); P(DelimArgs { dspan, delim: Delimiter::Brace, tokens }) } else { - return self.unexpected(); + self.unexpected_any()? }; self.psess.gated_spans.gate(sym::decl_macro, lo.to(self.prev_token.span)); @@ -2692,7 +2692,7 @@ impl<'a> Parser<'a> { debug!("parse_param_general parse_pat (is_name_required:{})", is_name_required); let (pat, colon) = this.parse_fn_param_pat_colon()?; if !colon { - let mut err = this.unexpected::<()>().unwrap_err(); + let mut err = this.unexpected().unwrap_err(); return if let Some(ident) = this.parameter_without_type(&mut err, pat, is_name_required, first_param) { @@ -2716,7 +2716,7 @@ impl<'a> Parser<'a> { { // This wasn't actually a type, but a pattern looking like a type, // so we are going to rollback and re-parse for recovery. - ty = this.unexpected(); + ty = this.unexpected_any(); } match ty { Ok(ty) => { diff --git a/compiler/rustc_parse/src/parser/mod.rs b/compiler/rustc_parse/src/parser/mod.rs index 12879dc429bc9..125e77d8ee7c4 100644 --- a/compiler/rustc_parse/src/parser/mod.rs +++ b/compiler/rustc_parse/src/parser/mod.rs @@ -465,7 +465,9 @@ impl<'a> Parser<'a> { matches!(self.recovery, Recovery::Allowed) } - pub fn unexpected(&mut self) -> PResult<'a, T> { + /// Version of [`unexpected`](Parser::unexpected) that "returns" any type in the `Ok` + /// (both those functions never return "Ok", and so can lie like that in the type). + pub fn unexpected_any(&mut self) -> PResult<'a, T> { match self.expect_one_of(&[], &[]) { Err(e) => Err(e), // We can get `Ok(true)` from `recover_closing_delimiter` @@ -474,6 +476,10 @@ impl<'a> Parser<'a> { } } + pub fn unexpected(&mut self) -> PResult<'a, ()> { + self.unexpected_any() + } + /// Expects and consumes the token `t`. Signals an error if the next token is not `t`. pub fn expect(&mut self, t: &TokenKind) -> PResult<'a, Recovered> { if self.expected_tokens.is_empty() { @@ -1278,7 +1284,11 @@ impl<'a> Parser<'a> { } fn parse_delim_args(&mut self) -> PResult<'a, P> { - if let Some(args) = self.parse_delim_args_inner() { Ok(P(args)) } else { self.unexpected() } + if let Some(args) = self.parse_delim_args_inner() { + Ok(P(args)) + } else { + self.unexpected_any() + } } fn parse_attr_args(&mut self) -> PResult<'a, AttrArgs> { diff --git a/compiler/rustc_parse/src/parser/path.rs b/compiler/rustc_parse/src/parser/path.rs index 163d10d03f03c..7dd6cd0e80672 100644 --- a/compiler/rustc_parse/src/parser/path.rs +++ b/compiler/rustc_parse/src/parser/path.rs @@ -660,7 +660,7 @@ impl<'a> Parser<'a> { // Add `>` to the list of expected tokens. self.check(&token::Gt); // Handle `,` to `;` substitution - let mut err = self.unexpected::<()>().unwrap_err(); + let mut err = self.unexpected().unwrap_err(); self.bump(); err.span_suggestion_verbose( self.prev_token.span.until(self.token.span), From 75d940f6379572631209a64791cdb317a0279dac Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Fri, 15 Mar 2024 11:37:42 +0000 Subject: [PATCH 2/2] Use `do yeet ()` and `do yeet _` instead of `None?` and `Err(_)?` in compiler This prevents breakage when `?` no longer skews inference. --- compiler/rustc_expand/src/lib.rs | 1 + compiler/rustc_expand/src/module.rs | 2 +- .../rustc_infer/src/infer/error_reporting/need_type_info.rs | 4 ++-- compiler/rustc_infer/src/lib.rs | 1 + 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_expand/src/lib.rs b/compiler/rustc_expand/src/lib.rs index e550f7242c338..94852884d6ba5 100644 --- a/compiler/rustc_expand/src/lib.rs +++ b/compiler/rustc_expand/src/lib.rs @@ -12,6 +12,7 @@ #![feature(proc_macro_internals)] #![feature(proc_macro_span)] #![feature(try_blocks)] +#![feature(yeet_expr)] #![allow(rustc::diagnostic_outside_of_impl)] #![allow(internal_features)] diff --git a/compiler/rustc_expand/src/module.rs b/compiler/rustc_expand/src/module.rs index 8a68b39e4965e..c8983619e7016 100644 --- a/compiler/rustc_expand/src/module.rs +++ b/compiler/rustc_expand/src/module.rs @@ -62,7 +62,7 @@ pub(crate) fn parse_external_mod( // Ensure file paths are acyclic. if let Some(pos) = module.file_path_stack.iter().position(|p| p == &mp.file_path) { - Err(ModError::CircularInclusion(module.file_path_stack[pos..].to_vec()))?; + do yeet ModError::CircularInclusion(module.file_path_stack[pos..].to_vec()); } // Actually parse the external file as a module. diff --git a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs index 13e2152e45ea1..0686994b0378d 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs @@ -990,7 +990,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> { let generics_def_id = tcx.res_generics_def_id(path.res)?; let generics = tcx.generics_of(generics_def_id); if generics.has_impl_trait() { - None?; + do yeet (); } let insert_span = path.segments.last().unwrap().ident.span.shrink_to_hi().with_hi(path.span.hi()); @@ -1044,7 +1044,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> { let generics = tcx.generics_of(def_id); let segment: Option<_> = try { if !segment.infer_args || generics.has_impl_trait() { - None?; + do yeet (); } let span = tcx.hir().span(segment.hir_id); let insert_span = segment.ident.span.shrink_to_hi().with_hi(span.hi()); diff --git a/compiler/rustc_infer/src/lib.rs b/compiler/rustc_infer/src/lib.rs index 029bddda1e1c5..97e3dfe8d0743 100644 --- a/compiler/rustc_infer/src/lib.rs +++ b/compiler/rustc_infer/src/lib.rs @@ -27,6 +27,7 @@ #![feature(iterator_try_collect)] #![cfg_attr(bootstrap, feature(min_specialization))] #![feature(try_blocks)] +#![feature(yeet_expr)] #![recursion_limit = "512"] // For rustdoc #[macro_use]