From e2ce5d74a5f87f9fb6baadd191288025ed974296 Mon Sep 17 00:00:00 2001 From: yukang Date: Wed, 21 Feb 2024 09:37:45 +0800 Subject: renaming test cases --- .../rustc_hir_typeck/src/fn_ctxt/suggestions.rs | 39 ------- .../rustc_infer/src/infer/error_reporting/mod.rs | 2 +- .../src/infer/error_reporting/suggest.rs | 10 +- tests/ui/inference/issue-105431-stmts-as-exp.rs | 76 ------------ .../ui/inference/issue-105431-stmts-as-exp.stderr | 129 --------------------- tests/ui/inference/stmts-as-exp-105431.rs | 76 ++++++++++++ tests/ui/inference/stmts-as-exp-105431.stderr | 129 +++++++++++++++++++++ 7 files changed, 211 insertions(+), 250 deletions(-) delete mode 100644 tests/ui/inference/issue-105431-stmts-as-exp.rs delete mode 100644 tests/ui/inference/issue-105431-stmts-as-exp.stderr create mode 100644 tests/ui/inference/stmts-as-exp-105431.rs create mode 100644 tests/ui/inference/stmts-as-exp-105431.stderr diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index d5161b00fbc..809102557ac 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -1791,45 +1791,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } - /// A common error is to add an extra semicolon: - /// - /// ```compile_fail,E0308 - /// fn foo() -> usize { - /// 22; - /// } - /// ``` - /// - /// This routine checks if the final statement in a block is an - /// expression with an explicit semicolon whose type is compatible - /// with `expected_ty`. If so, it suggests removing the semicolon. - pub(crate) fn consider_removing_semicolon( - &self, - blk: &'tcx hir::Block<'tcx>, - expected_ty: Ty<'tcx>, - err: &mut Diag<'_>, - ) -> bool { - if let Some((span_semi, boxed)) = self.err_ctxt().could_remove_semicolon(blk, expected_ty) { - if let StatementAsExpression::NeedsBoxing = boxed { - err.span_suggestion_verbose( - span_semi, - "consider removing this semicolon and boxing the expression", - "", - Applicability::HasPlaceholders, - ); - } else { - err.span_suggestion_short( - span_semi, - "remove this semicolon to return this value", - "", - Applicability::MachineApplicable, - ); - } - true - } else { - false - } - } - pub(crate) fn is_field_suggestable( &self, field: &ty::FieldDef, diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 6bb8c119942..e19879ae06d 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -1976,7 +1976,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { self.suggest_accessing_field_where_appropriate(cause, &exp_found, diag); self.suggest_await_on_expect_found(cause, span, &exp_found, diag); self.suggest_function_pointers(cause, span, &exp_found, diag); - self.suggest_for_statments_as_exp(cause, &exp_found, diag); + self.suggest_turning_stmt_into_expr(cause, &exp_found, diag); } } diff --git a/compiler/rustc_infer/src/infer/error_reporting/suggest.rs b/compiler/rustc_infer/src/infer/error_reporting/suggest.rs index be2437bfd60..472dab639d5 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/suggest.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/suggest.rs @@ -298,11 +298,11 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { } } - pub(super) fn suggest_for_statments_as_exp( + pub(super) fn suggest_turning_stmt_into_expr( &self, cause: &ObligationCause<'tcx>, exp_found: &ty::error::ExpectedFound>, - diag: &mut Diagnostic, + diag: &mut Diag<'_>, ) { let ty::error::ExpectedFound { expected, found } = exp_found; if !found.peel_refs().is_unit() { @@ -365,18 +365,18 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { &self, blk: &'tcx hir::Block<'tcx>, expected_ty: Ty<'tcx>, - err: &mut Diagnostic, + diag: &mut Diag<'_>, ) -> bool { if let Some((span_semi, boxed)) = self.could_remove_semicolon(blk, expected_ty) { if let StatementAsExpression::NeedsBoxing = boxed { - err.span_suggestion_verbose( + diag.span_suggestion_verbose( span_semi, "consider removing this semicolon and boxing the expression", "", Applicability::HasPlaceholders, ); } else { - err.span_suggestion_short( + diag.span_suggestion_short( span_semi, "remove this semicolon to return this value", "", diff --git a/tests/ui/inference/issue-105431-stmts-as-exp.rs b/tests/ui/inference/issue-105431-stmts-as-exp.rs deleted file mode 100644 index b5adb4a2b66..00000000000 --- a/tests/ui/inference/issue-105431-stmts-as-exp.rs +++ /dev/null @@ -1,76 +0,0 @@ -#![allow(unused)] - -fn test_if() -> i32 { - let x = if true { - eprintln!("hello"); - 3; - } - else { - 4; - }; - x //~ ERROR mismatched types -} - -fn test_if_without_binding() -> i32 { - if true { //~ ERROR mismatched types - eprintln!("hello"); - 3; - } - else { //~ ERROR mismatched types - 4; - } -} - -fn test_match() -> i32 { - let v = 1; - let res = match v { - 1 => { 1; } - _ => { 2; } - }; - res //~ ERROR mismatched types -} - -fn test_match_match_without_binding() -> i32 { - let v = 1; - match v { - 1 => { 1; } //~ ERROR mismatched types - _ => { 2; } //~ ERROR mismatched types - } -} - -fn test_match_arm_different_types() -> i32 { - let v = 1; - let res = match v { - 1 => { if 1 < 2 { 1 } else { 2 } } - _ => { 2; } //~ ERROR `match` arms have incompatible types - }; - res -} - -fn test_if_match_mixed() -> i32 { - let x = if true { - 3; - } else { - match 1 { - 1 => { 1 } - _ => { 2 } - }; - }; - x //~ ERROR mismatched types -} - -fn test_if_match_mixed_failed() -> i32 { - let x = if true { - 3; - } else { - // because this is a tailed expr, so we won't check deeper - match 1 { - 1 => { 33; } - _ => { 44; } - } - }; - x //~ ERROR mismatched types -} - - -fn main() {} diff --git a/tests/ui/inference/issue-105431-stmts-as-exp.stderr b/tests/ui/inference/issue-105431-stmts-as-exp.stderr deleted file mode 100644 index 2878d602ec5..00000000000 --- a/tests/ui/inference/issue-105431-stmts-as-exp.stderr +++ /dev/null @@ -1,129 +0,0 @@ -error[E0308]: mismatched types - --> $DIR/issue-105431-stmts-as-exp.rs:11:5 - | -LL | fn test_if() -> i32 { - | --- expected `i32` because of return type -... -LL | x - | ^ expected `i32`, found `()` - | -help: remove this semicolon to return this value - | -LL - 3; -LL + 3 - | -help: remove this semicolon to return this value - | -LL - 4; -LL + 4 - | - -error[E0308]: mismatched types - --> $DIR/issue-105431-stmts-as-exp.rs:15:13 - | -LL | if true { - | _____________^ -LL | | eprintln!("hello"); -LL | | 3; - | | - help: remove this semicolon to return this value -LL | | } - | |_____^ expected `i32`, found `()` - -error[E0308]: mismatched types - --> $DIR/issue-105431-stmts-as-exp.rs:19:10 - | -LL | else { - | __________^ -LL | | 4; - | | - help: remove this semicolon to return this value -LL | | } - | |_____^ expected `i32`, found `()` - -error[E0308]: mismatched types - --> $DIR/issue-105431-stmts-as-exp.rs:30:5 - | -LL | fn test_match() -> i32 { - | --- expected `i32` because of return type -... -LL | res - | ^^^ expected `i32`, found `()` - | -help: remove this semicolon to return this value - | -LL - 1 => { 1; } -LL + 1 => { 1 } - | -help: remove this semicolon to return this value - | -LL - _ => { 2; } -LL + _ => { 2 } - | - -error[E0308]: mismatched types - --> $DIR/issue-105431-stmts-as-exp.rs:36:14 - | -LL | 1 => { 1; } - | ^^^-^^ - | | | - | | help: remove this semicolon to return this value - | expected `i32`, found `()` - -error[E0308]: mismatched types - --> $DIR/issue-105431-stmts-as-exp.rs:37:14 - | -LL | _ => { 2; } - | ^^^-^^ - | | | - | | help: remove this semicolon to return this value - | expected `i32`, found `()` - -error[E0308]: `match` arms have incompatible types - --> $DIR/issue-105431-stmts-as-exp.rs:45:16 - | -LL | let res = match v { - | _______________- -LL | | 1 => { if 1 < 2 { 1 } else { 2 } } - | | ------------------------- this is found to be of type `{integer}` -LL | | _ => { 2; } - | | ^- - | | || - | | |help: consider removing this semicolon - | | expected integer, found `()` -LL | | }; - | |_____- `match` arms have incompatible types - -error[E0308]: mismatched types - --> $DIR/issue-105431-stmts-as-exp.rs:59:5 - | -LL | fn test_if_match_mixed() -> i32 { - | --- expected `i32` because of return type -... -LL | x - | ^ expected `i32`, found `()` - | -help: remove this semicolon to return this value - | -LL - 3; -LL + 3 - | -help: remove this semicolon to return this value - | -LL - }; -LL + } - | - -error[E0308]: mismatched types - --> $DIR/issue-105431-stmts-as-exp.rs:72:5 - | -LL | fn test_if_match_mixed_failed() -> i32 { - | --- expected `i32` because of return type -LL | let x = if true { -LL | 3; - | - help: remove this semicolon to return this value -... -LL | x - | ^ expected `i32`, found `()` - -error: aborting due to 9 previous errors - -For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/inference/stmts-as-exp-105431.rs b/tests/ui/inference/stmts-as-exp-105431.rs new file mode 100644 index 00000000000..b5adb4a2b66 --- /dev/null +++ b/tests/ui/inference/stmts-as-exp-105431.rs @@ -0,0 +1,76 @@ +#![allow(unused)] + +fn test_if() -> i32 { + let x = if true { + eprintln!("hello"); + 3; + } + else { + 4; + }; + x //~ ERROR mismatched types +} + +fn test_if_without_binding() -> i32 { + if true { //~ ERROR mismatched types + eprintln!("hello"); + 3; + } + else { //~ ERROR mismatched types + 4; + } +} + +fn test_match() -> i32 { + let v = 1; + let res = match v { + 1 => { 1; } + _ => { 2; } + }; + res //~ ERROR mismatched types +} + +fn test_match_match_without_binding() -> i32 { + let v = 1; + match v { + 1 => { 1; } //~ ERROR mismatched types + _ => { 2; } //~ ERROR mismatched types + } +} + +fn test_match_arm_different_types() -> i32 { + let v = 1; + let res = match v { + 1 => { if 1 < 2 { 1 } else { 2 } } + _ => { 2; } //~ ERROR `match` arms have incompatible types + }; + res +} + +fn test_if_match_mixed() -> i32 { + let x = if true { + 3; + } else { + match 1 { + 1 => { 1 } + _ => { 2 } + }; + }; + x //~ ERROR mismatched types +} + +fn test_if_match_mixed_failed() -> i32 { + let x = if true { + 3; + } else { + // because this is a tailed expr, so we won't check deeper + match 1 { + 1 => { 33; } + _ => { 44; } + } + }; + x //~ ERROR mismatched types +} + + +fn main() {} diff --git a/tests/ui/inference/stmts-as-exp-105431.stderr b/tests/ui/inference/stmts-as-exp-105431.stderr new file mode 100644 index 00000000000..f3da04b39a3 --- /dev/null +++ b/tests/ui/inference/stmts-as-exp-105431.stderr @@ -0,0 +1,129 @@ +error[E0308]: mismatched types + --> $DIR/stmts-as-exp-105431.rs:11:5 + | +LL | fn test_if() -> i32 { + | --- expected `i32` because of return type +... +LL | x + | ^ expected `i32`, found `()` + | +help: remove this semicolon to return this value + | +LL - 3; +LL + 3 + | +help: remove this semicolon to return this value + | +LL - 4; +LL + 4 + | + +error[E0308]: mismatched types + --> $DIR/stmts-as-exp-105431.rs:15:13 + | +LL | if true { + | _____________^ +LL | | eprintln!("hello"); +LL | | 3; + | | - help: remove this semicolon to return this value +LL | | } + | |_____^ expected `i32`, found `()` + +error[E0308]: mismatched types + --> $DIR/stmts-as-exp-105431.rs:19:10 + | +LL | else { + | __________^ +LL | | 4; + | | - help: remove this semicolon to return this value +LL | | } + | |_____^ expected `i32`, found `()` + +error[E0308]: mismatched types + --> $DIR/stmts-as-exp-105431.rs:30:5 + | +LL | fn test_match() -> i32 { + | --- expected `i32` because of return type +... +LL | res + | ^^^ expected `i32`, found `()` + | +help: remove this semicolon to return this value + | +LL - 1 => { 1; } +LL + 1 => { 1 } + | +help: remove this semicolon to return this value + | +LL - _ => { 2; } +LL + _ => { 2 } + | + +error[E0308]: mismatched types + --> $DIR/stmts-as-exp-105431.rs:36:14 + | +LL | 1 => { 1; } + | ^^^-^^ + | | | + | | help: remove this semicolon to return this value + | expected `i32`, found `()` + +error[E0308]: mismatched types + --> $DIR/stmts-as-exp-105431.rs:37:14 + | +LL | _ => { 2; } + | ^^^-^^ + | | | + | | help: remove this semicolon to return this value + | expected `i32`, found `()` + +error[E0308]: `match` arms have incompatible types + --> $DIR/stmts-as-exp-105431.rs:45:16 + | +LL | let res = match v { + | _______________- +LL | | 1 => { if 1 < 2 { 1 } else { 2 } } + | | ------------------------- this is found to be of type `{integer}` +LL | | _ => { 2; } + | | ^- + | | || + | | |help: consider removing this semicolon + | | expected integer, found `()` +LL | | }; + | |_____- `match` arms have incompatible types + +error[E0308]: mismatched types + --> $DIR/stmts-as-exp-105431.rs:59:5 + | +LL | fn test_if_match_mixed() -> i32 { + | --- expected `i32` because of return type +... +LL | x + | ^ expected `i32`, found `()` + | +help: remove this semicolon to return this value + | +LL - 3; +LL + 3 + | +help: remove this semicolon to return this value + | +LL - }; +LL + } + | + +error[E0308]: mismatched types + --> $DIR/stmts-as-exp-105431.rs:72:5 + | +LL | fn test_if_match_mixed_failed() -> i32 { + | --- expected `i32` because of return type +LL | let x = if true { +LL | 3; + | - help: remove this semicolon to return this value +... +LL | x + | ^ expected `i32`, found `()` + +error: aborting due to 9 previous errors + +For more information about this error, try `rustc --explain E0308`. -- cgit 1.4.1-3-g733a5