Skip to content

Commit a983dd8

Browse files
committed
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.
1 parent e78913b commit a983dd8

26 files changed

+128
-98
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -672,23 +672,21 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
672672
};
673673

674674
let assign_value = match ty.kind() {
675+
ty::Never | ty::Error(_) => return,
675676
ty::Bool => "false",
676677
ty::Float(_) => "0.0",
677678
ty::Int(_) | ty::Uint(_) => "0",
678-
ty::Never | ty::Error(_) => "",
679679
ty::Adt(def, _) if Some(def.did()) == tcx.get_diagnostic_item(sym::Vec) => "vec![]",
680680
ty::Adt(_, _) if implements_default(ty, self.param_env) => "Default::default()",
681-
_ => "todo!()",
681+
_ => "value",
682682
};
683683

684-
if !assign_value.is_empty() {
685-
err.span_suggestion_verbose(
686-
sugg_span.shrink_to_hi(),
687-
"consider assigning a value",
688-
format!(" = {assign_value}"),
689-
Applicability::MaybeIncorrect,
690-
);
691-
}
684+
err.span_suggestion_verbose(
685+
sugg_span.shrink_to_hi(),
686+
"consider assigning a value",
687+
format!(" = {assign_value}"),
688+
Applicability::MaybeIncorrect,
689+
);
692690
}
693691

694692
fn suggest_borrow_fn_like(

compiler/rustc_hir_analysis/src/check/check.rs

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use rustc_middle::ty::{
2222
AdtDef, ParamEnv, RegionKind, TypeSuperVisitable, TypeVisitable, TypeVisitableExt,
2323
};
2424
use rustc_session::lint::builtin::{UNINHABITED_STATIC, UNSUPPORTED_CALLING_CONVENTIONS};
25-
use rustc_span::symbol::sym;
2625
use rustc_target::abi::FieldIdx;
2726
use rustc_trait_selection::traits::error_reporting::on_unimplemented::OnUnimplementedDirective;
2827
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;

compiler/rustc_hir_analysis/src/check/mod.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,11 @@ use rustc_middle::ty::error::{ExpectedFound, TypeError};
9191
use rustc_middle::ty::{self, Ty, TyCtxt};
9292
use rustc_middle::ty::{GenericArgs, GenericArgsRef};
9393
use rustc_session::parse::feature_err;
94-
use rustc_span::symbol::{kw, Ident};
95-
use rustc_span::{self, def_id::CRATE_DEF_ID, BytePos, Span, Symbol, DUMMY_SP};
94+
use rustc_span::symbol::{kw, sym, Ident};
95+
use rustc_span::{def_id::CRATE_DEF_ID, BytePos, Span, Symbol, DUMMY_SP};
9696
use rustc_target::abi::VariantIdx;
9797
use rustc_target::spec::abi::Abi;
98+
use rustc_trait_selection::infer::InferCtxtExt;
9899
use rustc_trait_selection::traits::error_reporting::suggestions::ReturnsVisitor;
99100
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
100101
use rustc_trait_selection::traits::ObligationCtxt;
@@ -466,13 +467,24 @@ fn fn_sig_suggestion<'tcx>(
466467
)
467468
}
468469

469-
pub fn ty_kind_suggestion(ty: Ty<'_>) -> Option<&'static str> {
470+
pub fn ty_kind_suggestion<'tcx>(ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> Option<&'static str> {
471+
let implements_default = |ty| {
472+
let Some(default_trait) = tcx.get_diagnostic_item(sym::Default) else {
473+
return false;
474+
};
475+
let infcx = tcx.infer_ctxt().build();
476+
infcx
477+
.type_implements_trait(default_trait, [ty], ty::ParamEnv::reveal_all())
478+
.must_apply_modulo_regions()
479+
};
470480
Some(match ty.kind() {
471481
ty::Bool => "true",
472482
ty::Char => "'a'",
473483
ty::Int(_) | ty::Uint(_) => "42",
474484
ty::Float(_) => "3.14159",
475485
ty::Error(_) | ty::Never => return None,
486+
ty::Adt(def, _) if Some(def.did()) == tcx.get_diagnostic_item(sym::Vec) => "vec![]",
487+
ty::Adt(_, _) if implements_default(ty) => "Default::default()",
476488
_ => "value",
477489
})
478490
}
@@ -511,7 +523,7 @@ fn suggestion_signature<'tcx>(
511523
}
512524
ty::AssocKind::Const => {
513525
let ty = tcx.type_of(assoc.def_id).instantiate_identity();
514-
let val = ty_kind_suggestion(ty).unwrap_or("todo!()");
526+
let val = ty_kind_suggestion(ty, tcx).unwrap_or("todo!()");
515527
format!("const {}: {} = {};", assoc.name, ty, val)
516528
}
517529
}

compiler/rustc_hir_typeck/src/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -694,10 +694,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
694694
);
695695
let error = Some(Sorts(ExpectedFound { expected: ty, found: e_ty }));
696696
self.annotate_loop_expected_due_to_inference(err, expr, error);
697-
if let Some(val) = ty_kind_suggestion(ty) {
697+
if let Some(val) = ty_kind_suggestion(ty, tcx) {
698698
err.span_suggestion_verbose(
699699
expr.span.shrink_to_hi(),
700-
"give it a value of the expected type",
700+
"give the `break` a value of the expected type",
701701
format!(" {val}"),
702702
Applicability::HasPlaceholders,
703703
);

tests/ui/binop/issue-77910-1.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ LL | xs
3232
|
3333
help: consider assigning a value
3434
|
35-
LL | let xs = todo!();
36-
| +++++++++
35+
LL | let xs = value;
36+
| +++++++
3737

3838
error: aborting due to 3 previous errors
3939

tests/ui/binop/issue-77910-2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ LL | xs
2121
|
2222
help: consider assigning a value
2323
|
24-
LL | let xs = todo!();
25-
| +++++++++
24+
LL | let xs = value;
25+
| +++++++
2626

2727
error: aborting due to 2 previous errors
2828

tests/ui/borrowck/borrowck-init-in-fru.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ LL | origin = Point { x: 10, ..origin };
88
|
99
help: consider assigning a value
1010
|
11-
LL | let mut origin: Point = todo!();
12-
| +++++++++
11+
LL | let mut origin: Point = value;
12+
| +++++++
1313

1414
error: aborting due to 1 previous error
1515

tests/ui/borrowck/borrowck-uninit-ref-chain.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ LL | let _y = &**x;
88
|
99
help: consider assigning a value
1010
|
11-
LL | let x: &&Box<i32> = todo!();
12-
| +++++++++
11+
LL | let x: &&Box<i32> = value;
12+
| +++++++
1313

1414
error[E0381]: used binding `x` isn't initialized
1515
--> $DIR/borrowck-uninit-ref-chain.rs:11:14
@@ -21,8 +21,8 @@ LL | let _y = &**x;
2121
|
2222
help: consider assigning a value
2323
|
24-
LL | let x: &&S<i32, i32> = todo!();
25-
| +++++++++
24+
LL | let x: &&S<i32, i32> = value;
25+
| +++++++
2626

2727
error[E0381]: used binding `x` isn't initialized
2828
--> $DIR/borrowck-uninit-ref-chain.rs:14:14
@@ -34,8 +34,8 @@ LL | let _y = &**x;
3434
|
3535
help: consider assigning a value
3636
|
37-
LL | let x: &&i32 = todo!();
38-
| +++++++++
37+
LL | let x: &&i32 = value;
38+
| +++++++
3939

4040
error[E0381]: partially assigned binding `a` isn't fully initialized
4141
--> $DIR/borrowck-uninit-ref-chain.rs:18:5

tests/ui/borrowck/borrowck-use-in-index-lvalue.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ LL | w[5] = 0;
88
|
99
help: consider assigning a value
1010
|
11-
LL | let w: &mut [isize] = todo!();
12-
| +++++++++
11+
LL | let w: &mut [isize] = value;
12+
| +++++++
1313

1414
error[E0381]: used binding `w` isn't initialized
1515
--> $DIR/borrowck-use-in-index-lvalue.rs:6:5
@@ -21,8 +21,8 @@ LL | w[5] = 0;
2121
|
2222
help: consider assigning a value
2323
|
24-
LL | let mut w: &mut [isize] = todo!();
25-
| +++++++++
24+
LL | let mut w: &mut [isize] = value;
25+
| +++++++
2626

2727
error: aborting due to 2 previous errors
2828

tests/ui/borrowck/borrowck-use-uninitialized-in-cast-trait.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ LL | let y = x as *const dyn Foo;
88
|
99
help: consider assigning a value
1010
|
11-
LL | let x: &i32 = todo!();
12-
| +++++++++
11+
LL | let x: &i32 = value;
12+
| +++++++
1313

1414
error: aborting due to 1 previous error
1515

tests/ui/borrowck/borrowck-use-uninitialized-in-cast.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ LL | let y = x as *const i32;
88
|
99
help: consider assigning a value
1010
|
11-
LL | let x: &i32 = todo!();
12-
| +++++++++
11+
LL | let x: &i32 = value;
12+
| +++++++
1313

1414
error: aborting due to 1 previous error
1515

tests/ui/borrowck/issue-103250.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ LL | Err(last_error)
99
|
1010
help: consider assigning a value
1111
|
12-
LL | let mut last_error: Box<dyn std::error::Error> = todo!();
13-
| +++++++++
12+
LL | let mut last_error: Box<dyn std::error::Error> = value;
13+
| +++++++
1414

1515
error: aborting due to 1 previous error
1616

tests/ui/borrowck/suggest-assign-rvalue.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ LL | println!("demo_no: {:?}", demo_no);
5050
= 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)
5151
help: consider assigning a value
5252
|
53-
LL | let demo_no: DemoNoDef = todo!();
54-
| +++++++++
53+
LL | let demo_no: DemoNoDef = value;
54+
| +++++++
5555

5656
error[E0381]: used binding `arr` isn't initialized
5757
--> $DIR/suggest-assign-rvalue.rs:34:27
@@ -64,8 +64,8 @@ LL | println!("arr: {:?}", arr);
6464
= 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)
6565
help: consider assigning a value
6666
|
67-
LL | let arr: [i32; 5] = todo!();
68-
| +++++++++
67+
LL | let arr: [i32; 5] = value;
68+
| +++++++
6969

7070
error[E0381]: used binding `foo` isn't initialized
7171
--> $DIR/suggest-assign-rvalue.rs:37:27
@@ -106,8 +106,8 @@ LL | println!("my_int: {}", *my_int);
106106
= 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)
107107
help: consider assigning a value
108108
|
109-
LL | let my_int: &i32 = todo!();
110-
| +++++++++
109+
LL | let my_int: &i32 = value;
110+
| +++++++
111111

112112
error[E0381]: used binding `hello` isn't initialized
113113
--> $DIR/suggest-assign-rvalue.rs:49:27
@@ -120,8 +120,8 @@ LL | println!("hello: {}", hello);
120120
= 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)
121121
help: consider assigning a value
122122
|
123-
LL | let hello: &str = todo!();
124-
| +++++++++
123+
LL | let hello: &str = value;
124+
| +++++++
125125

126126
error[E0381]: used binding `never` isn't initialized
127127
--> $DIR/suggest-assign-rvalue.rs:53:27

tests/ui/const-generics/const-generic-default-wont-borrowck.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ LL | let s: &'static str; s.len()
88
|
99
help: consider assigning a value
1010
|
11-
LL | let s: &'static str = todo!(); s.len()
12-
| +++++++++
11+
LL | let s: &'static str = value; s.len()
12+
| +++++++
1313

1414
error: aborting due to 1 previous error
1515

tests/ui/issues/issue-27042.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ LL | loop { break };
1919
| |
2020
| this loop is expected to be of type `i32`
2121
|
22-
help: give it a value of the expected type
22+
help: give the `break` a value of the expected type
2323
|
2424
LL | loop { break 42 };
2525
| ++

tests/ui/loops/loop-break-value.rs

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ fn main() {
2323
};
2424
};
2525

26+
let _: Option<String> = loop {
27+
break; //~ ERROR mismatched types
28+
};
29+
2630
'while_loop: while true { //~ WARN denote infinite loops with
2731
break;
2832
break (); //~ ERROR `break` with value from a `while` loop

0 commit comments

Comments
 (0)