Skip to content

Commit e17388b

Browse files
committed
Handle more cases of value suggestions
1 parent a983dd8 commit e17388b

File tree

42 files changed

+273
-133
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+273
-133
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+51-18
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,55 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
652652
err
653653
}
654654

655+
fn ty_kind_suggestion(&self, ty: Ty<'tcx>) -> Option<String> {
656+
// Keep in sync with `rustc_hir_analysis/src/check/mod.rs:ty_kind_suggestion`.
657+
// FIXME: deduplicate the above.
658+
let implements_default = |ty| {
659+
let Some(default_trait) = self.infcx.tcx.get_diagnostic_item(sym::Default) else {
660+
return false;
661+
};
662+
self.infcx
663+
.type_implements_trait(default_trait, [ty], self.param_env)
664+
.must_apply_modulo_regions()
665+
};
666+
667+
Some(match ty.kind() {
668+
ty::Never | ty::Error(_) => return None,
669+
ty::Bool => "false".to_string(),
670+
ty::Char => "\'x\'".to_string(),
671+
ty::Int(_) | ty::Uint(_) => "42".into(),
672+
ty::Float(_) => "3.14159".into(),
673+
ty::Slice(_) => "[]".to_string(),
674+
ty::Adt(def, _) if Some(def.did()) == self.infcx.tcx.get_diagnostic_item(sym::Vec) => {
675+
"vec![]".to_string()
676+
}
677+
ty::Adt(_, _) if implements_default(ty) => "Default::default()".to_string(),
678+
ty::Ref(_, ty, mutability) => {
679+
if let (ty::Str, hir::Mutability::Not) = (ty.kind(), mutability) {
680+
"\"\"".to_string()
681+
} else {
682+
let Some(ty) = self.ty_kind_suggestion(*ty) else {
683+
return None;
684+
};
685+
format!("&{}{ty}", mutability.prefix_str())
686+
}
687+
}
688+
ty::Array(ty, len) => format!(
689+
"[{}; {}]",
690+
self.ty_kind_suggestion(*ty)?,
691+
len.eval_target_usize(self.infcx.tcx, ty::ParamEnv::reveal_all()),
692+
),
693+
ty::Tuple(tys) => format!(
694+
"({})",
695+
tys.iter()
696+
.map(|ty| self.ty_kind_suggestion(ty))
697+
.collect::<Option<Vec<String>>>()?
698+
.join(", ")
699+
),
700+
_ => "value".to_string(),
701+
})
702+
}
703+
655704
fn suggest_assign_value(
656705
&self,
657706
err: &mut Diag<'_>,
@@ -661,24 +710,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
661710
let ty = moved_place.ty(self.body, self.infcx.tcx).ty;
662711
debug!("ty: {:?}, kind: {:?}", ty, ty.kind());
663712

664-
let tcx = self.infcx.tcx;
665-
let implements_default = |ty, param_env| {
666-
let Some(default_trait) = tcx.get_diagnostic_item(sym::Default) else {
667-
return false;
668-
};
669-
self.infcx
670-
.type_implements_trait(default_trait, [ty], param_env)
671-
.must_apply_modulo_regions()
672-
};
673-
674-
let assign_value = match ty.kind() {
675-
ty::Never | ty::Error(_) => return,
676-
ty::Bool => "false",
677-
ty::Float(_) => "0.0",
678-
ty::Int(_) | ty::Uint(_) => "0",
679-
ty::Adt(def, _) if Some(def.did()) == tcx.get_diagnostic_item(sym::Vec) => "vec![]",
680-
ty::Adt(_, _) if implements_default(ty, self.param_env) => "Default::default()",
681-
_ => "value",
713+
let Some(assign_value) = self.ty_kind_suggestion(ty) else {
714+
return;
682715
};
683716

684717
err.span_suggestion_verbose(

compiler/rustc_hir_analysis/src/check/mod.rs

+38-10
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ use rustc_errors::ErrorGuaranteed;
8181
use rustc_errors::{pluralize, struct_span_code_err, Diag};
8282
use rustc_hir::def_id::{DefId, LocalDefId};
8383
use rustc_hir::intravisit::Visitor;
84+
use rustc_hir::Mutability;
8485
use rustc_index::bit_set::BitSet;
8586
use rustc_infer::infer::error_reporting::ObligationCauseExt as _;
8687
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
@@ -467,7 +468,9 @@ fn fn_sig_suggestion<'tcx>(
467468
)
468469
}
469470

470-
pub fn ty_kind_suggestion<'tcx>(ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> Option<&'static str> {
471+
pub fn ty_kind_suggestion<'tcx>(ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> Option<String> {
472+
// Keep in sync with `rustc_borrowck/src/diagnostics/conflict_errors.rs:ty_kind_suggestion`.
473+
// FIXME: deduplicate the above.
471474
let implements_default = |ty| {
472475
let Some(default_trait) = tcx.get_diagnostic_item(sym::Default) else {
473476
return false;
@@ -478,14 +481,39 @@ pub fn ty_kind_suggestion<'tcx>(ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> Option<&'sta
478481
.must_apply_modulo_regions()
479482
};
480483
Some(match ty.kind() {
481-
ty::Bool => "true",
482-
ty::Char => "'a'",
483-
ty::Int(_) | ty::Uint(_) => "42",
484-
ty::Float(_) => "3.14159",
485-
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()",
488-
_ => "value",
484+
ty::Never | ty::Error(_) => return None,
485+
ty::Bool => "false".to_string(),
486+
ty::Char => "\'x\'".to_string(),
487+
ty::Int(_) | ty::Uint(_) => "42".into(),
488+
ty::Float(_) => "3.14159".into(),
489+
ty::Slice(_) => "[]".to_string(),
490+
ty::Adt(def, _) if Some(def.did()) == tcx.get_diagnostic_item(sym::Vec) => {
491+
"vec![]".to_string()
492+
}
493+
ty::Adt(_, _) if implements_default(ty) => "Default::default()".to_string(),
494+
ty::Ref(_, ty, mutability) => {
495+
if let (ty::Str, Mutability::Not) = (ty.kind(), mutability) {
496+
"\"\"".to_string()
497+
} else {
498+
let Some(ty) = ty_kind_suggestion(*ty, tcx) else {
499+
return None;
500+
};
501+
format!("&{}{ty}", mutability.prefix_str())
502+
}
503+
}
504+
ty::Array(ty, len) => format!(
505+
"[{}; {}]",
506+
ty_kind_suggestion(*ty, tcx)?,
507+
len.eval_target_usize(tcx, ty::ParamEnv::reveal_all()),
508+
),
509+
ty::Tuple(tys) => format!(
510+
"({})",
511+
tys.iter()
512+
.map(|ty| ty_kind_suggestion(ty, tcx))
513+
.collect::<Option<Vec<String>>>()?
514+
.join(", ")
515+
),
516+
_ => "value".to_string(),
489517
})
490518
}
491519

@@ -523,7 +551,7 @@ fn suggestion_signature<'tcx>(
523551
}
524552
ty::AssocKind::Const => {
525553
let ty = tcx.type_of(assoc.def_id).instantiate_identity();
526-
let val = ty_kind_suggestion(ty, tcx).unwrap_or("todo!()");
554+
let val = ty_kind_suggestion(ty, tcx).unwrap_or_else(|| "value".to_string());
527555
format!("const {}: {} = {};", assoc.name, ty, val)
528556
}
529557
}

tests/ui/asm/x86_64/type-check-5.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ LL | asm!("{}", in(reg) x);
88
|
99
help: consider assigning a value
1010
|
11-
LL | let x: u64 = 0;
12-
| +++
11+
LL | let x: u64 = 42;
12+
| ++++
1313

1414
error[E0381]: used binding `y` isn't initialized
1515
--> $DIR/type-check-5.rs:18:9
@@ -21,8 +21,8 @@ LL | asm!("{}", inout(reg) y);
2121
|
2222
help: consider assigning a value
2323
|
24-
LL | let mut y: u64 = 0;
25-
| +++
24+
LL | let mut y: u64 = 42;
25+
| ++++
2626

2727
error[E0596]: cannot borrow `v` as mutable, as it is not declared as mutable
2828
--> $DIR/type-check-5.rs:24:13

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 = value;
36-
| +++++++
35+
LL | let xs = &42;
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 = value;
25-
| +++++++
24+
LL | let xs = &42;
25+
| +++++
2626

2727
error: aborting due to 2 previous errors
2828

tests/ui/borrowck/borrowck-block-uninit.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ LL | println!("{}", x);
1010
|
1111
help: consider assigning a value
1212
|
13-
LL | let x: isize = 0;
14-
| +++
13+
LL | let x: isize = 42;
14+
| ++++
1515

1616
error: aborting due to 1 previous error
1717

tests/ui/borrowck/borrowck-break-uninit-2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ LL | println!("{}", x);
1010
= 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)
1111
help: consider assigning a value
1212
|
13-
LL | let x: isize = 0;
14-
| +++
13+
LL | let x: isize = 42;
14+
| ++++
1515

1616
error: aborting due to 1 previous error
1717

tests/ui/borrowck/borrowck-break-uninit.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ LL | println!("{}", x);
1010
= 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)
1111
help: consider assigning a value
1212
|
13-
LL | let x: isize = 0;
14-
| +++
13+
LL | let x: isize = 42;
14+
| ++++
1515

1616
error: aborting due to 1 previous error
1717

tests/ui/borrowck/borrowck-init-in-called-fn-expr.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ LL | i
88
|
99
help: consider assigning a value
1010
|
11-
LL | let i: isize = 0;
12-
| +++
11+
LL | let i: isize = 42;
12+
| ++++
1313

1414
error: aborting due to 1 previous error
1515

tests/ui/borrowck/borrowck-init-in-fn-expr.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ LL | i
88
|
99
help: consider assigning a value
1010
|
11-
LL | let i: isize = 0;
12-
| +++
11+
LL | let i: isize = 42;
12+
| ++++
1313

1414
error: aborting due to 1 previous error
1515

tests/ui/borrowck/borrowck-init-op-equal.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ LL | v += 1;
88
|
99
help: consider assigning a value
1010
|
11-
LL | let v: isize = 0;
12-
| +++
11+
LL | let v: isize = 42;
12+
| ++++
1313

1414
error: aborting due to 1 previous error
1515

tests/ui/borrowck/borrowck-init-plus-equal.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ LL | v = v + 1;
88
|
99
help: consider assigning a value
1010
|
11-
LL | let mut v: isize = 0;
12-
| +++
11+
LL | let mut v: isize = 42;
12+
| ++++
1313

1414
error: aborting due to 1 previous error
1515

tests/ui/borrowck/borrowck-return.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ LL | return x;
88
|
99
help: consider assigning a value
1010
|
11-
LL | let x: isize = 0;
12-
| +++
11+
LL | let x: isize = 42;
12+
| ++++
1313

1414
error: aborting due to 1 previous error
1515

tests/ui/borrowck/borrowck-storage-dead.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ LL | let _ = x + 1;
88
|
99
help: consider assigning a value
1010
|
11-
LL | let x: i32 = 0;
12-
| +++
11+
LL | let x: i32 = 42;
12+
| ++++
1313

1414
error: aborting due to 1 previous error
1515

tests/ui/borrowck/borrowck-uninit-after-item.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ LL | baz(bar);
99
|
1010
help: consider assigning a value
1111
|
12-
LL | let bar = 0;
13-
| +++
12+
LL | let bar = 42;
13+
| ++++
1414

1515
error: aborting due to 1 previous error
1616

0 commit comments

Comments
 (0)