From 3e8ba946769701f7fc912e4b18bdc89aa54e0529 Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Sat, 12 Sep 2026 00:16:33 -0400 Subject: [PATCH] Remove some code duplication in coerce_shared.rs --- .../src/coherence/builtin/coerce_shared.rs | 25 ++++++------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/coherence/builtin/coerce_shared.rs b/compiler/rustc_hir_analysis/src/coherence/builtin/coerce_shared.rs index bd7130ee48bc8..637075f4822ff 100644 --- a/compiler/rustc_hir_analysis/src/coherence/builtin/coerce_shared.rs +++ b/compiler/rustc_hir_analysis/src/coherence/builtin/coerce_shared.rs @@ -400,29 +400,20 @@ fn validate_reborrow_field_access( ) -> Result<(), ErrorGuaranteed> { let module = tcx.parent_module_from_def_id(impl_did); let variant = def.non_enum_variant(); - if variant.field_list_has_applicable_non_exhaustive() { - return Err(tcx.dcx().emit_err(diagnostics::CoerceSharedInaccessibleField { + + if variant.field_list_has_applicable_non_exhaustive() + || variant.fields.iter().any(|f| !f.vis.is_accessible_from(module, tcx)) + { + Err(tcx.dcx().emit_err(diagnostics::CoerceSharedInaccessibleField { span: diagnostic_context.impl_span, type_span: role.type_span(diagnostic_context), trait_name, role: role.as_str(), type_name: tcx.item_name(def.did()), - })); - } - - for field in &variant.fields { - if !field.vis.is_accessible_from(module, tcx) { - return Err(tcx.dcx().emit_err(diagnostics::CoerceSharedInaccessibleField { - span: diagnostic_context.impl_span, - type_span: role.type_span(diagnostic_context), - trait_name, - role: role.as_str(), - type_name: tcx.item_name(def.did()), - })); - } + })) + } else { + Ok(()) } - - Ok(()) } fn validate_coerce_shared_fields<'tcx>(