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

Lines changed: 8 additions & 10 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 16 additions & 4 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 6 additions & 6 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 2 additions & 2 deletions
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

0 commit comments

Comments
 (0)