@@ -348,7 +348,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
348
348
}
349
349
}
350
350
351
- let compatible_variants: Vec < String > = expected_adt
351
+ let compatible_variants: Vec < ( String , Option < String > ) > = expected_adt
352
352
. variants ( )
353
353
. iter ( )
354
354
. filter ( |variant| {
@@ -357,14 +357,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
357
357
. filter_map ( |variant| {
358
358
let sole_field = & variant. fields [ 0 ] ;
359
359
360
- if ! sole_field. did . is_local ( )
361
- && !sole_field
362
- . vis
363
- . is_accessible_from ( expr . hir_id . owner . to_def_id ( ) , self . tcx )
364
- {
360
+ let field_is_local = sole_field. did . is_local ( ) ;
361
+ let field_is_accessible =
362
+ sole_field . vis . is_accessible_from ( expr . hir_id . owner . to_def_id ( ) , self . tcx ) ;
363
+
364
+ if !field_is_local && !field_is_accessible {
365
365
return None ;
366
366
}
367
367
368
+ let note_about_variant_field_privacy = ( field_is_local && !field_is_accessible)
369
+ . then ( || format ! ( " (its field is private, but it's local to this crate and its privacy can be changed)" ) ) ;
370
+
368
371
let sole_field_ty = sole_field. ty ( self . tcx , substs) ;
369
372
if self . can_coerce ( expr_ty, sole_field_ty) {
370
373
let variant_path =
@@ -373,9 +376,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
373
376
if let Some ( path) = variant_path. strip_prefix ( "std::prelude::" )
374
377
&& let Some ( ( _, path) ) = path. split_once ( "::" )
375
378
{
376
- return Some ( path. to_string ( ) ) ;
379
+ return Some ( ( path. to_string ( ) , note_about_variant_field_privacy ) ) ;
377
380
}
378
- Some ( variant_path)
381
+ Some ( ( variant_path, note_about_variant_field_privacy ) )
379
382
} else {
380
383
None
381
384
}
@@ -389,10 +392,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
389
392
390
393
match & compatible_variants[ ..] {
391
394
[ ] => { /* No variants to format */ }
392
- [ variant] => {
395
+ [ ( variant, note ) ] => {
393
396
// Just a single matching variant.
394
397
err. multipart_suggestion_verbose (
395
- & format ! ( "try wrapping the expression in `{variant}`" ) ,
398
+ & format ! (
399
+ "try wrapping the expression in `{variant}`{note}" ,
400
+ note = note. as_deref( ) . unwrap_or( "" )
401
+ ) ,
396
402
vec ! [
397
403
( expr. span. shrink_to_lo( ) , format!( "{prefix}{variant}(" ) ) ,
398
404
( expr. span. shrink_to_hi( ) , ")" . to_string( ) ) ,
@@ -407,7 +413,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
407
413
"try wrapping the expression in a variant of `{}`" ,
408
414
self . tcx. def_path_str( expected_adt. did( ) )
409
415
) ,
410
- compatible_variants. into_iter ( ) . map ( |variant| {
416
+ compatible_variants. into_iter ( ) . map ( |( variant, _ ) | {
411
417
vec ! [
412
418
( expr. span. shrink_to_lo( ) , format!( "{prefix}{variant}(" ) ) ,
413
419
( expr. span. shrink_to_hi( ) , ")" . to_string( ) ) ,
0 commit comments