Tweak value suggestions in borrowck and hir_analysis

Unify the output of `suggest_assign_value` and `ty_kind_suggestion`.

Ideally we'd make these a single function, but doing so would likely require modify the crate dependency tree.
This commit is contained in:
Esteban Küber 2024-04-09 23:37:01 +00:00
parent e78913baef
commit a983dd8563
26 changed files with 128 additions and 98 deletions

View File

@ -672,23 +672,21 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
};
let assign_value = match ty.kind() {
ty::Never | ty::Error(_) => return,
ty::Bool => "false",
ty::Float(_) => "0.0",
ty::Int(_) | ty::Uint(_) => "0",
ty::Never | ty::Error(_) => "",
ty::Adt(def, _) if Some(def.did()) == tcx.get_diagnostic_item(sym::Vec) => "vec![]",
ty::Adt(_, _) if implements_default(ty, self.param_env) => "Default::default()",
_ => "todo!()",
_ => "value",
};
if !assign_value.is_empty() {
err.span_suggestion_verbose(
sugg_span.shrink_to_hi(),
"consider assigning a value",
format!(" = {assign_value}"),
Applicability::MaybeIncorrect,
);
}
err.span_suggestion_verbose(
sugg_span.shrink_to_hi(),
"consider assigning a value",
format!(" = {assign_value}"),
Applicability::MaybeIncorrect,
);
}
fn suggest_borrow_fn_like(

View File

@ -22,7 +22,6 @@ use rustc_middle::ty::{
AdtDef, ParamEnv, RegionKind, TypeSuperVisitable, TypeVisitable, TypeVisitableExt,
};
use rustc_session::lint::builtin::{UNINHABITED_STATIC, UNSUPPORTED_CALLING_CONVENTIONS};
use rustc_span::symbol::sym;
use rustc_target::abi::FieldIdx;
use rustc_trait_selection::traits::error_reporting::on_unimplemented::OnUnimplementedDirective;
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;

View File

@ -91,10 +91,11 @@ use rustc_middle::ty::error::{ExpectedFound, TypeError};
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_middle::ty::{GenericArgs, GenericArgsRef};
use rustc_session::parse::feature_err;
use rustc_span::symbol::{kw, Ident};
use rustc_span::{self, def_id::CRATE_DEF_ID, BytePos, Span, Symbol, DUMMY_SP};
use rustc_span::symbol::{kw, sym, Ident};
use rustc_span::{def_id::CRATE_DEF_ID, BytePos, Span, Symbol, DUMMY_SP};
use rustc_target::abi::VariantIdx;
use rustc_target::spec::abi::Abi;
use rustc_trait_selection::infer::InferCtxtExt;
use rustc_trait_selection::traits::error_reporting::suggestions::ReturnsVisitor;
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
use rustc_trait_selection::traits::ObligationCtxt;
@ -466,13 +467,24 @@ fn fn_sig_suggestion<'tcx>(
)
}
pub fn ty_kind_suggestion(ty: Ty<'_>) -> Option<&'static str> {
pub fn ty_kind_suggestion<'tcx>(ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> Option<&'static str> {
let implements_default = |ty| {
let Some(default_trait) = tcx.get_diagnostic_item(sym::Default) else {
return false;
};
let infcx = tcx.infer_ctxt().build();
infcx
.type_implements_trait(default_trait, [ty], ty::ParamEnv::reveal_all())
.must_apply_modulo_regions()
};
Some(match ty.kind() {
ty::Bool => "true",
ty::Char => "'a'",
ty::Int(_) | ty::Uint(_) => "42",
ty::Float(_) => "3.14159",
ty::Error(_) | ty::Never => return None,
ty::Adt(def, _) if Some(def.did()) == tcx.get_diagnostic_item(sym::Vec) => "vec![]",
ty::Adt(_, _) if implements_default(ty) => "Default::default()",
_ => "value",
})
}
@ -511,7 +523,7 @@ fn suggestion_signature<'tcx>(
}
ty::AssocKind::Const => {
let ty = tcx.type_of(assoc.def_id).instantiate_identity();
let val = ty_kind_suggestion(ty).unwrap_or("todo!()");
let val = ty_kind_suggestion(ty, tcx).unwrap_or("todo!()");
format!("const {}: {} = {};", assoc.name, ty, val)
}
}

View File

@ -694,10 +694,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
);
let error = Some(Sorts(ExpectedFound { expected: ty, found: e_ty }));
self.annotate_loop_expected_due_to_inference(err, expr, error);
if let Some(val) = ty_kind_suggestion(ty) {
if let Some(val) = ty_kind_suggestion(ty, tcx) {
err.span_suggestion_verbose(
expr.span.shrink_to_hi(),
"give it a value of the expected type",
"give the `break` a value of the expected type",
format!(" {val}"),
Applicability::HasPlaceholders,
);

View File

@ -32,8 +32,8 @@ LL | xs
|
help: consider assigning a value
|
LL | let xs = todo!();
| +++++++++
LL | let xs = value;
| +++++++
error: aborting due to 3 previous errors

View File

@ -21,8 +21,8 @@ LL | xs
|
help: consider assigning a value
|
LL | let xs = todo!();
| +++++++++
LL | let xs = value;
| +++++++
error: aborting due to 2 previous errors

View File

@ -8,8 +8,8 @@ LL | origin = Point { x: 10, ..origin };
|
help: consider assigning a value
|
LL | let mut origin: Point = todo!();
| +++++++++
LL | let mut origin: Point = value;
| +++++++
error: aborting due to 1 previous error

View File

@ -8,8 +8,8 @@ LL | let _y = &**x;
|
help: consider assigning a value
|
LL | let x: &&Box<i32> = todo!();
| +++++++++
LL | let x: &&Box<i32> = value;
| +++++++
error[E0381]: used binding `x` isn't initialized
--> $DIR/borrowck-uninit-ref-chain.rs:11:14
@ -21,8 +21,8 @@ LL | let _y = &**x;
|
help: consider assigning a value
|
LL | let x: &&S<i32, i32> = todo!();
| +++++++++
LL | let x: &&S<i32, i32> = value;
| +++++++
error[E0381]: used binding `x` isn't initialized
--> $DIR/borrowck-uninit-ref-chain.rs:14:14
@ -34,8 +34,8 @@ LL | let _y = &**x;
|
help: consider assigning a value
|
LL | let x: &&i32 = todo!();
| +++++++++
LL | let x: &&i32 = value;
| +++++++
error[E0381]: partially assigned binding `a` isn't fully initialized
--> $DIR/borrowck-uninit-ref-chain.rs:18:5

View File

@ -8,8 +8,8 @@ LL | w[5] = 0;
|
help: consider assigning a value
|
LL | let w: &mut [isize] = todo!();
| +++++++++
LL | let w: &mut [isize] = value;
| +++++++
error[E0381]: used binding `w` isn't initialized
--> $DIR/borrowck-use-in-index-lvalue.rs:6:5
@ -21,8 +21,8 @@ LL | w[5] = 0;
|
help: consider assigning a value
|
LL | let mut w: &mut [isize] = todo!();
| +++++++++
LL | let mut w: &mut [isize] = value;
| +++++++
error: aborting due to 2 previous errors

View File

@ -8,8 +8,8 @@ LL | let y = x as *const dyn Foo;
|
help: consider assigning a value
|
LL | let x: &i32 = todo!();
| +++++++++
LL | let x: &i32 = value;
| +++++++
error: aborting due to 1 previous error

View File

@ -8,8 +8,8 @@ LL | let y = x as *const i32;
|
help: consider assigning a value
|
LL | let x: &i32 = todo!();
| +++++++++
LL | let x: &i32 = value;
| +++++++
error: aborting due to 1 previous error

View File

@ -9,8 +9,8 @@ LL | Err(last_error)
|
help: consider assigning a value
|
LL | let mut last_error: Box<dyn std::error::Error> = todo!();
| +++++++++
LL | let mut last_error: Box<dyn std::error::Error> = value;
| +++++++
error: aborting due to 1 previous error

View File

@ -50,8 +50,8 @@ LL | println!("demo_no: {:?}", demo_no);
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let demo_no: DemoNoDef = todo!();
| +++++++++
LL | let demo_no: DemoNoDef = value;
| +++++++
error[E0381]: used binding `arr` isn't initialized
--> $DIR/suggest-assign-rvalue.rs:34:27
@ -64,8 +64,8 @@ LL | println!("arr: {:?}", arr);
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let arr: [i32; 5] = todo!();
| +++++++++
LL | let arr: [i32; 5] = value;
| +++++++
error[E0381]: used binding `foo` isn't initialized
--> $DIR/suggest-assign-rvalue.rs:37:27
@ -106,8 +106,8 @@ LL | println!("my_int: {}", *my_int);
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let my_int: &i32 = todo!();
| +++++++++
LL | let my_int: &i32 = value;
| +++++++
error[E0381]: used binding `hello` isn't initialized
--> $DIR/suggest-assign-rvalue.rs:49:27
@ -120,8 +120,8 @@ LL | println!("hello: {}", hello);
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let hello: &str = todo!();
| +++++++++
LL | let hello: &str = value;
| +++++++
error[E0381]: used binding `never` isn't initialized
--> $DIR/suggest-assign-rvalue.rs:53:27

View File

@ -8,8 +8,8 @@ LL | let s: &'static str; s.len()
|
help: consider assigning a value
|
LL | let s: &'static str = todo!(); s.len()
| +++++++++
LL | let s: &'static str = value; s.len()
| +++++++
error: aborting due to 1 previous error

View File

@ -19,7 +19,7 @@ LL | loop { break };
| |
| this loop is expected to be of type `i32`
|
help: give it a value of the expected type
help: give the `break` a value of the expected type
|
LL | loop { break 42 };
| ++

View File

@ -23,6 +23,10 @@ fn main() {
};
};
let _: Option<String> = loop {
break; //~ ERROR mismatched types
};
'while_loop: while true { //~ WARN denote infinite loops with
break;
break (); //~ ERROR `break` with value from a `while` loop

View File

@ -1,5 +1,5 @@
warning: label name `'a` shadows a label name that is already in scope
--> $DIR/loop-break-value.rs:136:17
--> $DIR/loop-break-value.rs:140:17
|
LL | 'a: loop {
| -- first declared here
@ -8,7 +8,7 @@ LL | let _ = 'a: loop {
| ^^ label `'a` already in scope
warning: label name `'a` shadows a label name that is already in scope
--> $DIR/loop-break-value.rs:148:17
--> $DIR/loop-break-value.rs:152:17
|
LL | 'a: loop {
| -- first declared here
@ -17,7 +17,7 @@ LL | let _ = 'a: loop {
| ^^ label `'a` already in scope
error[E0425]: cannot find value `LOOP` in this scope
--> $DIR/loop-break-value.rs:95:15
--> $DIR/loop-break-value.rs:99:15
|
LL | 'LOOP: for _ in 0 .. 9 {
| ----- a label with a similar name exists
@ -28,7 +28,7 @@ LL | break LOOP;
| help: use the similarly named label: `'LOOP`
warning: denote infinite loops with `loop { ... }`
--> $DIR/loop-break-value.rs:26:5
--> $DIR/loop-break-value.rs:30:5
|
LL | 'while_loop: while true {
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use `loop`
@ -36,7 +36,7 @@ LL | 'while_loop: while true {
= note: `#[warn(while_true)]` on by default
error[E0571]: `break` with value from a `while` loop
--> $DIR/loop-break-value.rs:28:9
--> $DIR/loop-break-value.rs:32:9
|
LL | 'while_loop: while true {
| ----------------------- you can't `break` with a value in a `while` loop
@ -54,7 +54,7 @@ LL | break 'while_loop;
| ~~~~~~~~~~~
error[E0571]: `break` with value from a `while` loop
--> $DIR/loop-break-value.rs:30:13
--> $DIR/loop-break-value.rs:34:13
|
LL | 'while_loop: while true {
| ----------------------- you can't `break` with a value in a `while` loop
@ -68,7 +68,7 @@ LL | break 'while_loop;
| ~~~~~~~~~~~~~~~~~
error[E0571]: `break` with value from a `while` loop
--> $DIR/loop-break-value.rs:38:12
--> $DIR/loop-break-value.rs:42:12
|
LL | while let Some(_) = Some(()) {
| ---------------------------- you can't `break` with a value in a `while` loop
@ -81,7 +81,7 @@ LL | if break {
| ~~~~~
error[E0571]: `break` with value from a `while` loop
--> $DIR/loop-break-value.rs:43:9
--> $DIR/loop-break-value.rs:47:9
|
LL | while let Some(_) = Some(()) {
| ---------------------------- you can't `break` with a value in a `while` loop
@ -94,7 +94,7 @@ LL | break;
| ~~~~~
error[E0571]: `break` with value from a `while` loop
--> $DIR/loop-break-value.rs:49:13
--> $DIR/loop-break-value.rs:53:13
|
LL | 'while_let_loop: while let Some(_) = Some(()) {
| --------------------------------------------- you can't `break` with a value in a `while` loop
@ -108,7 +108,7 @@ LL | break 'while_let_loop;
| ~~~~~~~~~~~~~~~~~~~~~
error[E0571]: `break` with value from a `for` loop
--> $DIR/loop-break-value.rs:56:9
--> $DIR/loop-break-value.rs:60:9
|
LL | for _ in &[1,2,3] {
| ----------------- you can't `break` with a value in a `for` loop
@ -121,7 +121,7 @@ LL | break;
| ~~~~~
error[E0571]: `break` with value from a `for` loop
--> $DIR/loop-break-value.rs:57:9
--> $DIR/loop-break-value.rs:61:9
|
LL | for _ in &[1,2,3] {
| ----------------- you can't `break` with a value in a `for` loop
@ -135,7 +135,7 @@ LL | break;
| ~~~~~
error[E0571]: `break` with value from a `for` loop
--> $DIR/loop-break-value.rs:64:13
--> $DIR/loop-break-value.rs:68:13
|
LL | 'for_loop: for _ in &[1,2,3] {
| ---------------------------- you can't `break` with a value in a `for` loop
@ -191,7 +191,24 @@ LL | break 'outer_loop "nope";
| ^^^^^^ expected `i32`, found `&str`
error[E0308]: mismatched types
--> $DIR/loop-break-value.rs:73:26
--> $DIR/loop-break-value.rs:27:9
|
LL | let _: Option<String> = loop {
| - ---- this loop is expected to be of type `Option<String>`
| |
| expected because of this assignment
LL | break;
| ^^^^^ expected `Option<String>`, found `()`
|
= note: expected enum `Option<String>`
found unit type `()`
help: give the `break` a value of the expected type
|
LL | break Default::default();
| ++++++++++++++++++
error[E0308]: mismatched types
--> $DIR/loop-break-value.rs:77:26
|
LL | break;
| ----- expected because of this `break`
@ -199,7 +216,7 @@ LL | break 'c 123;
| ^^^ expected `()`, found integer
error[E0308]: mismatched types
--> $DIR/loop-break-value.rs:80:15
--> $DIR/loop-break-value.rs:84:15
|
LL | break (break, break);
| ^-----^^-----^
@ -212,7 +229,7 @@ LL | break (break, break);
found tuple `(!, !)`
error[E0308]: mismatched types
--> $DIR/loop-break-value.rs:85:15
--> $DIR/loop-break-value.rs:89:15
|
LL | break;
| ----- expected because of this `break`
@ -220,20 +237,20 @@ LL | break 2;
| ^ expected `()`, found integer
error[E0308]: mismatched types
--> $DIR/loop-break-value.rs:90:9
--> $DIR/loop-break-value.rs:94:9
|
LL | break 2;
| ------- expected because of this `break`
LL | break;
| ^^^^^ expected integer, found `()`
|
help: give it a value of the expected type
help: give the `break` a value of the expected type
|
LL | break value;
| +++++
error[E0308]: mismatched types
--> $DIR/loop-break-value.rs:108:9
--> $DIR/loop-break-value.rs:112:9
|
LL | break 'a 1;
| ---------- expected because of this `break`
@ -241,13 +258,13 @@ LL | break 'a 1;
LL | break;
| ^^^^^ expected integer, found `()`
|
help: give it a value of the expected type
help: give the `break` a value of the expected type
|
LL | break value;
| +++++
error[E0308]: mismatched types
--> $DIR/loop-break-value.rs:120:9
--> $DIR/loop-break-value.rs:124:9
|
LL | break 'a 1;
| ---------- expected because of this `break`
@ -255,13 +272,13 @@ LL | break 'a 1;
LL | break 'a;
| ^^^^^^^^ expected integer, found `()`
|
help: give it a value of the expected type
help: give the `break` a value of the expected type
|
LL | break 'a value;
| +++++
error[E0308]: mismatched types
--> $DIR/loop-break-value.rs:131:15
--> $DIR/loop-break-value.rs:135:15
|
LL | break;
| ----- expected because of this `break`
@ -270,7 +287,7 @@ LL | break 2;
| ^ expected `()`, found integer
error[E0308]: mismatched types
--> $DIR/loop-break-value.rs:140:17
--> $DIR/loop-break-value.rs:144:17
|
LL | break 2;
| ------- expected because of this `break`
@ -278,13 +295,13 @@ LL | loop {
LL | break 'a;
| ^^^^^^^^ expected integer, found `()`
|
help: give it a value of the expected type
help: give the `break` a value of the expected type
|
LL | break 'a value;
| +++++
error[E0308]: mismatched types
--> $DIR/loop-break-value.rs:143:15
--> $DIR/loop-break-value.rs:147:15
|
LL | break;
| ----- expected because of this `break`
@ -293,7 +310,7 @@ LL | break 2;
| ^ expected `()`, found integer
error[E0308]: mismatched types
--> $DIR/loop-break-value.rs:152:17
--> $DIR/loop-break-value.rs:156:17
|
LL | break 'a 2;
| ---------- expected because of this `break`
@ -301,13 +318,13 @@ LL | loop {
LL | break 'a;
| ^^^^^^^^ expected integer, found `()`
|
help: give it a value of the expected type
help: give the `break` a value of the expected type
|
LL | break 'a value;
| +++++
error[E0308]: mismatched types
--> $DIR/loop-break-value.rs:155:15
--> $DIR/loop-break-value.rs:159:15
|
LL | break;
| ----- expected because of this `break`
@ -316,7 +333,7 @@ LL | break 2;
| ^ expected `()`, found integer
error[E0308]: mismatched types
--> $DIR/loop-break-value.rs:159:15
--> $DIR/loop-break-value.rs:163:15
|
LL | fn main() {
| - expected `()` because of this return type
@ -326,7 +343,7 @@ LL | loop { // point at the return type
LL | break 2;
| ^ expected `()`, found integer
error: aborting due to 25 previous errors; 3 warnings emitted
error: aborting due to 26 previous errors; 3 warnings emitted
Some errors have detailed explanations: E0308, E0425, E0571.
For more information about an error, try `rustc --explain E0308`.

View File

@ -7,7 +7,7 @@ LL | let _: i32 = loop { break };
| | this loop is expected to be of type `i32`
| expected because of this assignment
|
help: give it a value of the expected type
help: give the `break` a value of the expected type
|
LL | let _: i32 = loop { break 42 };
| ++
@ -21,7 +21,7 @@ LL | let _: i32 = 'inner: loop { break 'inner };
| | this loop is expected to be of type `i32`
| expected because of this assignment
|
help: give it a value of the expected type
help: give the `break` a value of the expected type
|
LL | let _: i32 = 'inner: loop { break 'inner 42 };
| ++
@ -35,7 +35,7 @@ LL | let _: i32 = 'inner2: loop { loop { break 'inner2 } };
| | this loop is expected to be of type `i32`
| expected because of this assignment
|
help: give it a value of the expected type
help: give the `break` a value of the expected type
|
LL | let _: i32 = 'inner2: loop { loop { break 'inner2 42 } };
| ++

View File

@ -7,7 +7,7 @@ LL | let x: i32 = loop { break };
| | this loop is expected to be of type `i32`
| expected because of this assignment
|
help: give it a value of the expected type
help: give the `break` a value of the expected type
|
LL | let x: i32 = loop { break 42 };
| ++

View File

@ -56,8 +56,8 @@ LL | let _used = value;
|
help: consider assigning a value
|
LL | let value: NonCopy = todo!();
| +++++++++
LL | let value: NonCopy = value;
| +++++++
error[E0381]: used binding `value` isn't initialized
--> $DIR/issue-72649-uninit-in-loop.rs:69:21
@ -70,8 +70,8 @@ LL | let _used = value;
|
help: consider assigning a value
|
LL | let mut value: NonCopy = todo!();
| +++++++++
LL | let mut value: NonCopy = value;
| +++++++
error: aborting due to 6 previous errors

View File

@ -8,8 +8,8 @@ LL | a[i] = d();
|
help: consider assigning a value
|
LL | let mut a: [D; 4] = todo!();
| +++++++++
LL | let mut a: [D; 4] = value;
| +++++++
error: aborting due to 1 previous error

View File

@ -9,8 +9,8 @@ LL | std::ptr::addr_of_mut!(x);
= note: this error originates in the macro `std::ptr::addr_of_mut` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let mut x: S = todo!();
| +++++++++
LL | let mut x: S = value;
| +++++++
error: aborting due to 1 previous error

View File

@ -36,7 +36,7 @@ error[E0308]: mismatched types
LL | [(); loop { break }];
| ^^^^^ expected `usize`, found `()`
|
help: give it a value of the expected type
help: give the `break` a value of the expected type
|
LL | [(); loop { break 42 }];
| ++

View File

@ -43,8 +43,8 @@ LL | match n {}
|
help: consider assigning a value
|
LL | let n: Never = todo!();
| +++++++++
LL | let n: Never = value;
| +++++++
error: aborting due to 4 previous errors

View File

@ -8,7 +8,7 @@ LL | loop {
LL | if false { break; }
| ^^^^^ expected `i32`, found `()`
|
help: give it a value of the expected type
help: give the `break` a value of the expected type
|
LL | if false { break 42; }
| ++

View File

@ -9,8 +9,8 @@ LL | *y = 2;
|
help: consider assigning a value
|
LL | let y: &mut u32 = todo!();
| +++++++++
LL | let y: &mut u32 = value;
| +++++++
error: aborting due to 1 previous error