diff options
| author | bors <bors@rust-lang.org> | 2024-04-15 19:19:50 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-04-15 19:19:50 +0000 |
| commit | 3493a56529b3f972205f2cdda920132deef4b475 (patch) | |
| tree | 2962dac55ec79d12f1d3881e230d902dd2725d23 /compiler/rustc_hir_analysis/src | |
| parent | 99d0186b1d0547eae913eff04be272c9d348b9b8 (diff) | |
| parent | 27cb6bcb9b699a91d69618c016e3a04330486976 (diff) | |
| download | rust-3493a56529b3f972205f2cdda920132deef4b475.tar.gz rust-3493a56529b3f972205f2cdda920132deef4b475.zip | |
Auto merge of #123982 - compiler-errors:rollup-m2v4epp, r=compiler-errors
Rollup of 4 pull requests Successful merges: - #123900 (Stop using `PolyTraitRef` for closure/coroutine predicates already instantiated w placeholders) - #123924 (Fix various bugs in `ty_kind_suggestion`) - #123943 (Use the rustc_private libc less in tests) - #123970 (zkvm: fix references to `os_str` module) r? `@ghost` `@rustbot` modify labels: rollup
Diffstat (limited to 'compiler/rustc_hir_analysis/src')
| -rw-r--r-- | compiler/rustc_hir_analysis/src/check/mod.rs | 74 |
1 files changed, 9 insertions, 65 deletions
diff --git a/compiler/rustc_hir_analysis/src/check/mod.rs b/compiler/rustc_hir_analysis/src/check/mod.rs index 024a0433b8c..eb0ffc19d45 100644 --- a/compiler/rustc_hir_analysis/src/check/mod.rs +++ b/compiler/rustc_hir_analysis/src/check/mod.rs @@ -81,7 +81,6 @@ use rustc_errors::ErrorGuaranteed; use rustc_errors::{pluralize, struct_span_code_err, Diag}; use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::intravisit::Visitor; -use rustc_hir::Mutability; use rustc_index::bit_set::BitSet; use rustc_infer::infer::error_reporting::ObligationCauseExt as _; use rustc_infer::infer::outlives::env::OutlivesEnvironment; @@ -96,8 +95,9 @@ 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::suggestions::{ + ReturnsVisitor, TypeErrCtxtExt as _, +}; use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _; use rustc_trait_selection::traits::ObligationCtxt; @@ -467,67 +467,6 @@ fn fn_sig_suggestion<'tcx>( ) } -pub fn ty_kind_suggestion<'tcx>(ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> Option<String> { - // Keep in sync with `rustc_borrowck/src/diagnostics/conflict_errors.rs:ty_kind_suggestion`. - // FIXME: deduplicate the above. - 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::Never | ty::Error(_) => return None, - ty::Bool => "false".to_string(), - ty::Char => "\'x\'".to_string(), - ty::Int(_) | ty::Uint(_) => "42".into(), - ty::Float(_) => "3.14159".into(), - ty::Slice(_) => "[]".to_string(), - ty::Adt(def, _) if Some(def.did()) == tcx.get_diagnostic_item(sym::Vec) => { - "vec![]".to_string() - } - ty::Adt(def, _) if Some(def.did()) == tcx.get_diagnostic_item(sym::String) => { - "String::new()".to_string() - } - ty::Adt(def, args) if def.is_box() => { - format!("Box::new({})", ty_kind_suggestion(args[0].expect_ty(), tcx)?) - } - ty::Adt(def, _) if Some(def.did()) == tcx.get_diagnostic_item(sym::Option) => { - "None".to_string() - } - ty::Adt(def, args) if Some(def.did()) == tcx.get_diagnostic_item(sym::Result) => { - format!("Ok({})", ty_kind_suggestion(args[0].expect_ty(), tcx)?) - } - ty::Adt(_, _) if implements_default(ty) => "Default::default()".to_string(), - ty::Ref(_, ty, mutability) => { - if let (ty::Str, Mutability::Not) = (ty.kind(), mutability) { - "\"\"".to_string() - } else { - let Some(ty) = ty_kind_suggestion(*ty, tcx) else { - return None; - }; - format!("&{}{ty}", mutability.prefix_str()) - } - } - ty::Array(ty, len) => format!( - "[{}; {}]", - ty_kind_suggestion(*ty, tcx)?, - len.eval_target_usize(tcx, ty::ParamEnv::reveal_all()), - ), - ty::Tuple(tys) => format!( - "({})", - tys.iter() - .map(|ty| ty_kind_suggestion(ty, tcx)) - .collect::<Option<Vec<String>>>()? - .join(", ") - ), - _ => "value".to_string(), - }) -} - /// Return placeholder code for the given associated item. /// Similar to `ty::AssocItem::suggestion`, but appropriate for use as the code snippet of a /// structured suggestion. @@ -562,7 +501,12 @@ fn suggestion_signature<'tcx>( } ty::AssocKind::Const => { let ty = tcx.type_of(assoc.def_id).instantiate_identity(); - let val = ty_kind_suggestion(ty, tcx).unwrap_or_else(|| "value".to_string()); + let val = tcx + .infer_ctxt() + .build() + .err_ctxt() + .ty_kind_suggestion(tcx.param_env(assoc.def_id), ty) + .unwrap_or_else(|| "value".to_string()); format!("const {}: {} = {};", assoc.name, ty, val) } } |
