Skip to content

Commit 1f09bc7

Browse files
committed
Migrate (most of) report_and_explain_type_error
1 parent ab11b43 commit 1f09bc7

File tree

5 files changed

+397
-229
lines changed

5 files changed

+397
-229
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
768768
let (provided_ty, provided_span) = provided_arg_tys[*provided_idx];
769769
let trace =
770770
mk_trace(provided_span, formal_and_expected_inputs[*expected_idx], provided_ty);
771-
if !matches!(trace.cause.as_failure_code(*e), FailureCode::Error0308(_)) {
771+
if !matches!(trace.cause.as_failure_code(*e), FailureCode::Error0308) {
772772
self.err_ctxt().report_and_explain_type_error(trace, *e).emit();
773773
return true;
774774
}

compiler/rustc_infer/messages.ftl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,3 +370,25 @@ infer_stp_wrap_one = try wrapping the pattern in `{$variant}`
370370
infer_stp_wrap_many = try wrapping the pattern in a variant of `{$path}`
371371
372372
infer_tuple_trailing_comma = use a trailing comma to create a tuple with one element
373+
374+
infer_oc_method_compat = method not compatible with trait
375+
infer_oc_type_compat = type not compatible with trait
376+
infer_oc_const_compat = const not compatible with trait
377+
infer_oc_try_compat = `?` operator has incompatible types
378+
infer_oc_match_compat = `match` arms have incompatible types
379+
infer_oc_if_else_different = `if` and `else` have incompatible types
380+
infer_oc_no_else = `if` may be missing an `else` clause
381+
infer_oc_no_diverge = `else` clause of `let...else` does not diverge
382+
infer_oc_fn_main_correct_type = `main` function has wrong type
383+
infer_oc_fn_start_correct_type = `#[start]` function has wrong type
384+
infer_oc_intristic_correct_type = intrinsic has wrong type
385+
infer_oc_method_correct_type = mismatched `self` parameter type
386+
infer_oc_closure_selfref = closure/generator type that references itself
387+
infer_oc_cant_coerce = cannot coerce intrinsics to function pointers
388+
infer_oc_generic = mismatched types
389+
390+
infer_meant_byte_literal = if you meant to write a byte literal, prefix with `b`
391+
infer_meant_char_literal = if you meant to write a `char` literal, use single quotes
392+
infer_meant_str_literal = if you meant to write a `str` literal, use double quotes
393+
infer_consider_specifying_length = consider specifying the actual array length
394+
infer_try_cannot_convert = `?` operator cannot convert from `{$found}` to `{$expected}`

compiler/rustc_infer/src/errors/mod.rs

Lines changed: 158 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -184,18 +184,6 @@ pub enum SourceKindMultiSuggestion<'a> {
184184
},
185185
}
186186

187-
#[derive(Subdiagnostic)]
188-
#[suggestion(
189-
infer_suggest_add_let_for_letchains,
190-
style = "verbose",
191-
applicability = "machine-applicable",
192-
code = "let "
193-
)]
194-
pub(crate) struct SuggAddLetForLetChains {
195-
#[primary_span]
196-
pub span: Span,
197-
}
198-
199187
impl<'a> SourceKindMultiSuggestion<'a> {
200188
pub fn new_fully_qualified(
201189
span: Span,
@@ -1373,17 +1361,172 @@ impl AddToDiagnostic for SuggestTuplePatternMany {
13731361
}
13741362

13751363
#[derive(Subdiagnostic)]
1376-
pub enum TupleTrailingCommaSuggestion {
1364+
pub enum Error0308Subdiags {
1365+
#[suggestion(
1366+
infer_meant_byte_literal,
1367+
code = "b'{code}'",
1368+
applicability = "machine-applicable"
1369+
)]
1370+
MeantByteLiteral {
1371+
#[primary_span]
1372+
span: Span,
1373+
code: String,
1374+
},
1375+
#[suggestion(
1376+
infer_meant_char_literal,
1377+
code = "'{code}'",
1378+
applicability = "machine-applicable"
1379+
)]
1380+
MeantCharLiteral {
1381+
#[primary_span]
1382+
span: Span,
1383+
code: String,
1384+
},
1385+
#[suggestion(
1386+
infer_meant_str_literal,
1387+
code = "\"{code}\"",
1388+
applicability = "machine-applicable"
1389+
)]
1390+
MeantStrLiteral {
1391+
#[primary_span]
1392+
span: Span,
1393+
code: String,
1394+
},
1395+
#[suggestion(
1396+
infer_consider_specifying_length,
1397+
code = "{length}",
1398+
applicability = "maybe-incorrect"
1399+
)]
1400+
ConsiderSpecifyingLength {
1401+
#[primary_span]
1402+
span: Span,
1403+
length: u64,
1404+
},
1405+
#[note(infer_try_cannot_convert)]
1406+
TryCannotConvert { found: String, expected: String },
13771407
#[suggestion(infer_tuple_trailing_comma, code = ",", applicability = "machine-applicable")]
1378-
OnlyComma {
1408+
TupleOnlyComma {
13791409
#[primary_span]
13801410
span: Span,
13811411
},
13821412
#[multipart_suggestion(infer_tuple_trailing_comma, applicability = "machine-applicable")]
1383-
AlsoParentheses {
1413+
TupleAlsoParentheses {
13841414
#[suggestion_part(code = "(")]
13851415
span_low: Span,
13861416
#[suggestion_part(code = ",)")]
13871417
span_high: Span,
13881418
},
1419+
#[suggestion(
1420+
infer_suggest_add_let_for_letchains,
1421+
style = "verbose",
1422+
applicability = "machine-applicable",
1423+
code = "let "
1424+
)]
1425+
AddLetForLetChains {
1426+
#[primary_span]
1427+
span: Span,
1428+
},
1429+
}
1430+
1431+
#[derive(Diagnostic)]
1432+
pub enum FailureCodeDiagnostics {
1433+
#[diag(infer_oc_method_compat, code = "E0308")]
1434+
MethodCompat {
1435+
#[primary_span]
1436+
span: Span,
1437+
#[subdiagnostic]
1438+
subdiags: Vec<Error0308Subdiags>,
1439+
},
1440+
#[diag(infer_oc_type_compat, code = "E0308")]
1441+
TypeCompat {
1442+
#[primary_span]
1443+
span: Span,
1444+
#[subdiagnostic]
1445+
subdiags: Vec<Error0308Subdiags>,
1446+
},
1447+
#[diag(infer_oc_const_compat, code = "E0308")]
1448+
ConstCompat {
1449+
#[primary_span]
1450+
span: Span,
1451+
#[subdiagnostic]
1452+
subdiags: Vec<Error0308Subdiags>,
1453+
},
1454+
#[diag(infer_oc_try_compat, code = "E0308")]
1455+
TryCompat {
1456+
#[primary_span]
1457+
span: Span,
1458+
#[subdiagnostic]
1459+
subdiags: Vec<Error0308Subdiags>,
1460+
},
1461+
#[diag(infer_oc_match_compat, code = "E0308")]
1462+
MatchCompat {
1463+
#[primary_span]
1464+
span: Span,
1465+
#[subdiagnostic]
1466+
subdiags: Vec<Error0308Subdiags>,
1467+
},
1468+
#[diag(infer_oc_if_else_different, code = "E0308")]
1469+
IfElseDifferent {
1470+
#[primary_span]
1471+
span: Span,
1472+
#[subdiagnostic]
1473+
subdiags: Vec<Error0308Subdiags>,
1474+
},
1475+
#[diag(infer_oc_no_else, code = "E0317")]
1476+
NoElse {
1477+
#[primary_span]
1478+
span: Span,
1479+
},
1480+
#[diag(infer_oc_no_diverge, code = "E0308")]
1481+
NoDiverge {
1482+
#[primary_span]
1483+
span: Span,
1484+
#[subdiagnostic]
1485+
subdiags: Vec<Error0308Subdiags>,
1486+
},
1487+
#[diag(infer_oc_fn_main_correct_type, code = "E0580")]
1488+
FnMainCorrectType {
1489+
#[primary_span]
1490+
span: Span,
1491+
},
1492+
#[diag(infer_oc_fn_start_correct_type, code = "E0308")]
1493+
FnStartCorrectType {
1494+
#[primary_span]
1495+
span: Span,
1496+
#[subdiagnostic]
1497+
subdiags: Vec<Error0308Subdiags>,
1498+
},
1499+
#[diag(infer_oc_intristic_correct_type, code = "E0308")]
1500+
IntristicCorrectType {
1501+
#[primary_span]
1502+
span: Span,
1503+
#[subdiagnostic]
1504+
subdiags: Vec<Error0308Subdiags>,
1505+
},
1506+
#[diag(infer_oc_method_correct_type, code = "E0308")]
1507+
MethodCorrectType {
1508+
#[primary_span]
1509+
span: Span,
1510+
#[subdiagnostic]
1511+
subdiags: Vec<Error0308Subdiags>,
1512+
},
1513+
#[diag(infer_oc_closure_selfref, code = "E0644")]
1514+
ClosureSelfref {
1515+
#[primary_span]
1516+
span: Span,
1517+
},
1518+
#[diag(infer_oc_cant_coerce, code = "E0308")]
1519+
CantCoerce {
1520+
#[primary_span]
1521+
span: Span,
1522+
#[subdiagnostic]
1523+
subdiags: Vec<Error0308Subdiags>,
1524+
},
1525+
#[diag(infer_oc_generic, code = "E0308")]
1526+
Generic {
1527+
#[primary_span]
1528+
span: Span,
1529+
#[subdiagnostic]
1530+
subdiags: Vec<Error0308Subdiags>,
1531+
},
13891532
}

0 commit comments

Comments
 (0)