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