Add a note about privacy to wrapping suggestion
This commit is contained in:
parent
2edad7d77c
commit
5bd88dfa8a
2 changed files with 18 additions and 12 deletions
|
@ -348,7 +348,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let compatible_variants: Vec<String> = expected_adt
|
let compatible_variants: Vec<(String, Option<String>)> = expected_adt
|
||||||
.variants()
|
.variants()
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|variant| {
|
.filter(|variant| {
|
||||||
|
@ -357,14 +357,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
.filter_map(|variant| {
|
.filter_map(|variant| {
|
||||||
let sole_field = &variant.fields[0];
|
let sole_field = &variant.fields[0];
|
||||||
|
|
||||||
if !sole_field.did.is_local()
|
let field_is_local = sole_field.did.is_local();
|
||||||
&& !sole_field
|
let field_is_accessible =
|
||||||
.vis
|
sole_field.vis.is_accessible_from(expr.hir_id.owner.to_def_id(), self.tcx);
|
||||||
.is_accessible_from(expr.hir_id.owner.to_def_id(), self.tcx)
|
|
||||||
{
|
if !field_is_local && !field_is_accessible {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let note_about_variant_field_privacy = (field_is_local && !field_is_accessible)
|
||||||
|
.then(|| format!(" (its field is private, but it's local to this crate and its privacy can be changed)"));
|
||||||
|
|
||||||
let sole_field_ty = sole_field.ty(self.tcx, substs);
|
let sole_field_ty = sole_field.ty(self.tcx, substs);
|
||||||
if self.can_coerce(expr_ty, sole_field_ty) {
|
if self.can_coerce(expr_ty, sole_field_ty) {
|
||||||
let variant_path =
|
let variant_path =
|
||||||
|
@ -373,9 +376,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
if let Some(path) = variant_path.strip_prefix("std::prelude::")
|
if let Some(path) = variant_path.strip_prefix("std::prelude::")
|
||||||
&& let Some((_, path)) = path.split_once("::")
|
&& let Some((_, path)) = path.split_once("::")
|
||||||
{
|
{
|
||||||
return Some(path.to_string());
|
return Some((path.to_string(), note_about_variant_field_privacy));
|
||||||
}
|
}
|
||||||
Some(variant_path)
|
Some((variant_path, note_about_variant_field_privacy))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -389,10 +392,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
|
|
||||||
match &compatible_variants[..] {
|
match &compatible_variants[..] {
|
||||||
[] => { /* No variants to format */ }
|
[] => { /* No variants to format */ }
|
||||||
[variant] => {
|
[(variant, note)] => {
|
||||||
// Just a single matching variant.
|
// Just a single matching variant.
|
||||||
err.multipart_suggestion_verbose(
|
err.multipart_suggestion_verbose(
|
||||||
&format!("try wrapping the expression in `{variant}`"),
|
&format!(
|
||||||
|
"try wrapping the expression in `{variant}`{note}",
|
||||||
|
note = note.as_deref().unwrap_or("")
|
||||||
|
),
|
||||||
vec![
|
vec![
|
||||||
(expr.span.shrink_to_lo(), format!("{prefix}{variant}(")),
|
(expr.span.shrink_to_lo(), format!("{prefix}{variant}(")),
|
||||||
(expr.span.shrink_to_hi(), ")".to_string()),
|
(expr.span.shrink_to_hi(), ")".to_string()),
|
||||||
|
@ -407,7 +413,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
"try wrapping the expression in a variant of `{}`",
|
"try wrapping the expression in a variant of `{}`",
|
||||||
self.tcx.def_path_str(expected_adt.did())
|
self.tcx.def_path_str(expected_adt.did())
|
||||||
),
|
),
|
||||||
compatible_variants.into_iter().map(|variant| {
|
compatible_variants.into_iter().map(|(variant, _)| {
|
||||||
vec![
|
vec![
|
||||||
(expr.span.shrink_to_lo(), format!("{prefix}{variant}(")),
|
(expr.span.shrink_to_lo(), format!("{prefix}{variant}(")),
|
||||||
(expr.span.shrink_to_hi(), ")".to_string()),
|
(expr.span.shrink_to_hi(), ")".to_string()),
|
||||||
|
|
|
@ -13,7 +13,7 @@ note: function defined here
|
||||||
|
|
|
|
||||||
LL | fn needs_wrapper(t: inner::Wrapper<i32>) {}
|
LL | fn needs_wrapper(t: inner::Wrapper<i32>) {}
|
||||||
| ^^^^^^^^^^^^^ ----------------------
|
| ^^^^^^^^^^^^^ ----------------------
|
||||||
help: try wrapping the expression in `inner::Wrapper`
|
help: try wrapping the expression in `inner::Wrapper` (its field is private, but it's local to this crate and its privacy can be changed)
|
||||||
|
|
|
|
||||||
LL | needs_wrapper(inner::Wrapper(0));
|
LL | needs_wrapper(inner::Wrapper(0));
|
||||||
| +++++++++++++++ +
|
| +++++++++++++++ +
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue