Skip to content

Commit e17388b

Browse files
committedApr 10, 2024
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

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

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

0 commit comments

Comments
 (0)
Please sign in to comment.