Skip to content

Commit 71ebc61

Browse files
committed
resolve: Simplify fn report_privacy_error
by factoring out `fn ctor_fields_span` into a separate function
1 parent c20d7ee commit 71ebc61

File tree

1 file changed

+31
-36
lines changed

1 file changed

+31
-36
lines changed

src/librustc_resolve/diagnostics.rs

+31-36
Original file line numberDiff line numberDiff line change
@@ -916,51 +916,46 @@ impl<'a> Resolver<'a> {
916916
err.emit();
917917
}
918918

919-
crate fn report_privacy_error(&self, privacy_error: &PrivacyError<'_>) {
920-
let PrivacyError { ident, binding, .. } = *privacy_error;
921-
let session = &self.session;
922-
let mk_struct_span_error = |is_constructor| {
923-
let mut descr = binding.res().descr().to_string();
924-
if is_constructor {
925-
descr += " constructor";
926-
}
927-
if binding.is_import() {
928-
descr += " import";
929-
}
930-
931-
let mut err =
932-
struct_span_err!(session, ident.span, E0603, "{} `{}` is private", descr, ident);
933-
934-
err.span_label(ident.span, &format!("this {} is private", descr));
935-
err.span_note(
936-
session.source_map().def_span(binding.span),
937-
&format!("the {} `{}` is defined here", descr, ident),
938-
);
939-
940-
err
941-
};
942-
943-
let mut err = if let NameBindingKind::Res(
919+
/// If the binding refers to a tuple struct constructor with fields,
920+
/// returns the span of its fields.
921+
fn ctor_fields_span(&self, binding: &NameBinding<'_>) -> Option<Span> {
922+
if let NameBindingKind::Res(
944923
Res::Def(DefKind::Ctor(CtorOf::Struct, CtorKind::Fn), ctor_def_id),
945924
_,
946925
) = binding.kind
947926
{
948927
let def_id = (&*self).parent(ctor_def_id).expect("no parent for a constructor");
949928
if let Some(fields) = self.field_names.get(&def_id) {
950-
let mut err = mk_struct_span_error(true);
951929
let first_field = fields.first().expect("empty field list in the map");
952-
err.span_label(
953-
fields.iter().fold(first_field.span, |acc, field| acc.to(field.span)),
954-
"a constructor is private if any of the fields is private",
955-
);
956-
err
957-
} else {
958-
mk_struct_span_error(false)
930+
return Some(fields.iter().fold(first_field.span, |acc, field| acc.to(field.span)));
959931
}
960-
} else {
961-
mk_struct_span_error(false)
962-
};
932+
}
933+
None
934+
}
935+
936+
crate fn report_privacy_error(&self, privacy_error: &PrivacyError<'_>) {
937+
let PrivacyError { ident, binding, .. } = *privacy_error;
938+
939+
let ctor_fields_span = self.ctor_fields_span(binding);
940+
let mut descr = binding.res().descr().to_string();
941+
if ctor_fields_span.is_some() {
942+
descr += " constructor";
943+
}
944+
if binding.is_import() {
945+
descr += " import";
946+
}
963947

948+
let mut err =
949+
struct_span_err!(self.session, ident.span, E0603, "{} `{}` is private", descr, ident);
950+
err.span_label(ident.span, &format!("this {} is private", descr));
951+
if let Some(span) = ctor_fields_span {
952+
err.span_label(span, "a constructor is private if any of the fields is private");
953+
}
954+
955+
err.span_note(
956+
self.session.source_map().def_span(binding.span),
957+
&format!("the {} `{}` is defined here", descr, ident),
958+
);
964959
err.emit();
965960
}
966961
}

0 commit comments

Comments
 (0)