11use either:: Either ;
22use hir:: {
3- AssocItem , FindPathConfig , HirDisplay , InFile , Type ,
3+ AssocItem , FindPathConfig , HasVisibility , HirDisplay , InFile , Type ,
44 db:: { ExpandDatabase , HirDatabase } ,
55 sym,
66} ;
@@ -35,7 +35,7 @@ use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext, fix};
3535// ```
3636pub ( crate ) fn missing_fields ( ctx : & DiagnosticsContext < ' _ > , d : & hir:: MissingFields ) -> Diagnostic {
3737 let mut message = String :: from ( "missing structure fields:\n " ) ;
38- for field in & d. missed_fields {
38+ for ( field, _ ) in & d. missed_fields {
3939 format_to ! ( message, "- {}\n " , field. display( ctx. sema. db, ctx. edition) ) ;
4040 }
4141
@@ -57,7 +57,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
5757 // `struct A(usize);`
5858 // `let a = A { 0: () }`
5959 // but it is uncommon usage and it should not be encouraged.
60- if d. missed_fields . iter ( ) . any ( |it| it . as_tuple_index ( ) . is_some ( ) ) {
60+ if d. missed_fields . iter ( ) . any ( |( name , _ ) | name . as_tuple_index ( ) . is_some ( ) ) {
6161 return None ;
6262 }
6363
@@ -68,6 +68,12 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
6868 let range = InFile :: new ( d. file , d. field_list_parent . text_range ( ) )
6969 . original_node_file_range_rooted_opt ( ctx. sema . db ) ?;
7070
71+ if let Some ( current_module) = current_module
72+ && d. missed_fields . iter ( ) . any ( |( _, field) | !field. is_visible_from ( ctx. db ( ) , current_module) )
73+ {
74+ return None ;
75+ }
76+
7177 let build_text_edit = |new_syntax : & SyntaxNode , old_syntax| {
7278 let edit = {
7379 let old_range = ctx. sema . original_range_opt ( old_syntax) ?;
@@ -254,7 +260,7 @@ fn get_default_constructor(
254260
255261#[ cfg( test) ]
256262mod tests {
257- use crate :: tests:: { check_diagnostics, check_fix} ;
263+ use crate :: tests:: { check_diagnostics, check_fix, check_no_fix } ;
258264
259265 #[ test]
260266 fn missing_record_pat_field_diagnostic ( ) {
@@ -956,6 +962,21 @@ struct A {
956962fn f() -> A {
957963 let v = loop {};
958964 A { v }
965+ }
966+ "# ,
967+ ) ;
968+ }
969+
970+ #[ test]
971+ fn inaccessible_fields ( ) {
972+ check_no_fix (
973+ r#"
974+ mod foo {
975+ pub struct Bar { baz: i32 }
976+ }
977+
978+ fn qux() {
979+ foo::Bar {$0};
959980}
960981 "# ,
961982 ) ;
0 commit comments