diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index 0963ea71f8023..a5725a15701da 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -466,6 +466,7 @@ pub enum StashKey { /// When an invalid lifetime e.g. `'2` should be reinterpreted /// as a char literal in the parser LifetimeIsChar, + RangeLit, } fn default_track_diagnostic(_: &Diagnostic) {} diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index 9fde62a81a1a6..801160d9c27a4 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -1613,9 +1613,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.demand_coerce_diag(&field.expr, ty, field_type, None, AllowTwoPhase::No); if let Some(mut diag) = diag { - if idx == ast_fields.len() - 1 && remaining_fields.is_empty() { - self.suggest_fru_from_range(field, variant, substs, &mut diag); + if idx == ast_fields.len() - 1 { + if remaining_fields.is_empty() { + self.suggest_fru_from_range(field, variant, substs, &mut diag); + } else { + diag.clone().stash(diag.sort_span, StashKey::RangeLit); + } } + diag.emit(); } } @@ -1844,7 +1849,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ); err.span_label(span, format!("missing {remaining_fields_names}{truncated_fields_error}")); - if let Some(last) = ast_fields.last() { + if let Some(last) = ast_fields.last() + && self.suggest_fru_from_range(last, variant, substs, &mut err) { + // unstash and cancel + let un = self.tcx.sess.diagnostic().steal_diagnostic(span, StashKey::RangeLit).unwrap(); + un.cancel(); self.suggest_fru_from_range(last, variant, substs, &mut err); } @@ -1859,7 +1868,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { variant: &ty::VariantDef, substs: SubstsRef<'tcx>, err: &mut Diagnostic, - ) { + ) -> bool { // I don't use 'is_range_literal' because only double-sided, half-open ranges count. if let ExprKind::Struct( QPath::LangItem(LangItem::Range, ..), @@ -1887,7 +1896,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ",", Applicability::MaybeIncorrect, ); + + return true; } + + return false; } /// Report an error for a struct field expression when there are invisible fields.