diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2023-10-17 19:29:43 +0000 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2023-10-26 22:21:05 +0000 |
| commit | 87dc85d3222743aeb82848537ad77c2ece746ef6 (patch) | |
| tree | 020294eafbc78fb86f849ea25b573c08b693a054 /compiler | |
| parent | 93e62a260f9edb5813a73908120a43f1415f2c8c (diff) | |
| download | rust-87dc85d3222743aeb82848537ad77c2ece746ef6.tar.gz rust-87dc85d3222743aeb82848537ad77c2ece746ef6.zip | |
Suggest assoc fn `new` when trying to build tuple struct with private fields
Fix #22488.
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_resolve/src/late/diagnostics.rs | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index a5f8f61f3db..fd5d6fabf02 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -1570,7 +1570,26 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { err.set_primary_message( "cannot initialize a tuple struct which contains private fields", ); - + if !def_id.is_local() + && self + .r + .tcx + .inherent_impls(def_id) + .iter() + .flat_map(|impl_def_id| { + self.r.tcx.provided_trait_methods(*impl_def_id) + }) + .any(|assoc| !assoc.fn_has_self_parameter && assoc.name == sym::new) + { + // FIXME: look for associated functions with Self return type, + // instead of relying only on the name and lack of self receiver. + err.span_suggestion_verbose( + span.shrink_to_hi(), + "you might have meant to use the `new` associated function", + "::new".to_string(), + Applicability::MaybeIncorrect, + ); + } // Use spans of the tuple struct definition. self.r.field_def_ids(def_id).map(|field_ids| { field_ids |
