|
5 | 5 | use crate::cast; |
6 | 6 | use crate::coercion::CoerceMany; |
7 | 7 | use crate::coercion::DynamicCoerceMany; |
| 8 | +use crate::errors::TypeMismatchFruTypo; |
8 | 9 | use crate::errors::{AddressOfTemporaryTaken, ReturnStmtOutsideOfFnBody, StructExprNonExhaustive}; |
9 | 10 | use crate::errors::{ |
10 | 11 | FieldMultiplySpecifiedInInitializer, FunctionalRecordUpdateOnNonStruct, |
@@ -1614,10 +1615,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { |
1614 | 1615 | self.demand_coerce_diag(&field.expr, ty, field_type, None, AllowTwoPhase::No); |
1615 | 1616 |
|
1616 | 1617 | if let Some(mut diag) = diag { |
1617 | | - if idx == ast_fields.len() - 1 && remaining_fields.is_empty() { |
1618 | | - self.suggest_fru_from_range(field, variant, substs, &mut diag); |
| 1618 | + if idx == ast_fields.len() - 1 { |
| 1619 | + if remaining_fields.is_empty() { |
| 1620 | + self.suggest_fru_from_range(field, variant, substs, &mut diag); |
| 1621 | + diag.emit(); |
| 1622 | + } else { |
| 1623 | + diag.stash(field.span, StashKey::MaybeFruTypo); |
| 1624 | + } |
| 1625 | + } else { |
| 1626 | + diag.emit(); |
1619 | 1627 | } |
1620 | | - diag.emit(); |
1621 | 1628 | } |
1622 | 1629 | } |
1623 | 1630 |
|
@@ -1875,19 +1882,39 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { |
1875 | 1882 | .map(|adt| adt.did()) |
1876 | 1883 | != range_def_id |
1877 | 1884 | { |
1878 | | - let instead = self |
| 1885 | + // Suppress any range expr type mismatches |
| 1886 | + if let Some(mut diag) = self |
| 1887 | + .tcx |
| 1888 | + .sess |
| 1889 | + .diagnostic() |
| 1890 | + .steal_diagnostic(last_expr_field.span, StashKey::MaybeFruTypo) |
| 1891 | + { |
| 1892 | + diag.delay_as_bug(); |
| 1893 | + } |
| 1894 | + |
| 1895 | + // Use a (somewhat arbitrary) filtering heuristic to avoid printing |
| 1896 | + // expressions that are either too long, or have control character |
| 1897 | + //such as newlines in them. |
| 1898 | + let expr = self |
1879 | 1899 | .tcx |
1880 | 1900 | .sess |
1881 | 1901 | .source_map() |
1882 | 1902 | .span_to_snippet(range_end.expr.span) |
1883 | | - .map(|s| format!(" from `{s}`")) |
1884 | | - .unwrap_or_default(); |
1885 | | - err.span_suggestion( |
1886 | | - range_start.span.shrink_to_hi(), |
1887 | | - &format!("to set the remaining fields{instead}, separate the last named field with a comma"), |
1888 | | - ",", |
1889 | | - Applicability::MaybeIncorrect, |
1890 | | - ); |
| 1903 | + .ok() |
| 1904 | + .filter(|s| s.len() < 25 && !s.contains(|c: char| c.is_control())); |
| 1905 | + |
| 1906 | + let fru_span = self |
| 1907 | + .tcx |
| 1908 | + .sess |
| 1909 | + .source_map() |
| 1910 | + .span_extend_while(range_start.span, |c| c.is_whitespace()) |
| 1911 | + .unwrap_or(range_start.span).shrink_to_hi().to(range_end.span); |
| 1912 | + |
| 1913 | + err.subdiagnostic(TypeMismatchFruTypo { |
| 1914 | + expr_span: range_start.span, |
| 1915 | + fru_span, |
| 1916 | + expr, |
| 1917 | + }); |
1891 | 1918 | } |
1892 | 1919 | } |
1893 | 1920 |
|
|
0 commit comments