Skip to content

Commit 7d23c32

Browse files
Merge pull request #21977 from ChayimFriedman2/fill-fields-priv
fix: Disable the fix for missing-fields when the fields are private
2 parents 54d67b8 + 10ecf39 commit 7d23c32

3 files changed

Lines changed: 38 additions & 6 deletions

File tree

crates/hir/src/diagnostics.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ pub struct MissingFields {
282282
pub file: HirFileId,
283283
pub field_list_parent: AstPtr<Either<ast::RecordExpr, ast::RecordPat>>,
284284
pub field_list_parent_path: Option<AstPtr<ast::Path>>,
285-
pub missed_fields: Vec<Name>,
285+
pub missed_fields: Vec<(Name, Field)>,
286286
}
287287

288288
#[derive(Debug)]
@@ -476,7 +476,12 @@ impl<'db> AnyDiagnostic<'db> {
476476
let variant_data = variant.fields(db);
477477
let missed_fields = missed_fields
478478
.into_iter()
479-
.map(|idx| variant_data.fields()[idx].name.clone())
479+
.map(|idx| {
480+
(
481+
variant_data.fields()[idx].name.clone(),
482+
Field { parent: variant.into(), id: idx },
483+
)
484+
})
480485
.collect();
481486

482487
let record = match record {

crates/ide-diagnostics/src/handlers/missing_fields.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use either::Either;
22
use 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
// ```
3636
pub(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)]
256262
mod 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 {
956962
fn 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
);

crates/ide-diagnostics/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,12 @@ struct DiagnosticsContext<'a> {
285285
is_nightly: bool,
286286
}
287287

288+
impl<'a> DiagnosticsContext<'a> {
289+
fn db(&self) -> &'a RootDatabase {
290+
self.sema.db
291+
}
292+
}
293+
288294
/// Request parser level diagnostics for the given [`FileId`].
289295
pub fn syntax_diagnostics(
290296
db: &RootDatabase,

0 commit comments

Comments
 (0)