Delay formatting trimmed path until lint/error is emitted

This commit is contained in:
Michael Goulet 2022-07-29 06:48:39 +00:00
parent d77da9da84
commit 0ad57d8502
3 changed files with 51 additions and 14 deletions

View file

@ -1,3 +1,6 @@
use std::fmt::Display;
use rustc_errors::IntoDiagnosticArg;
use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic}; use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic};
use rustc_span::{Span, Symbol}; use rustc_span::{Span, Symbol};
@ -35,7 +38,7 @@ pub struct ItemIsPrivate<'a> {
#[label] #[label]
pub span: Span, pub span: Span,
pub kind: &'a str, pub kind: &'a str,
pub descr: String, pub descr: FromDisplay<'a>,
} }
#[derive(SessionDiagnostic)] #[derive(SessionDiagnostic)]
@ -55,7 +58,7 @@ pub struct InPublicInterfaceTraits<'a> {
pub span: Span, pub span: Span,
pub vis_descr: &'static str, pub vis_descr: &'static str,
pub kind: &'a str, pub kind: &'a str,
pub descr: String, pub descr: FromDisplay<'a>,
#[label(privacy::visibility_label)] #[label(privacy::visibility_label)]
pub vis_span: Span, pub vis_span: Span,
} }
@ -69,7 +72,7 @@ pub struct InPublicInterface<'a> {
pub span: Span, pub span: Span,
pub vis_descr: &'static str, pub vis_descr: &'static str,
pub kind: &'a str, pub kind: &'a str,
pub descr: String, pub descr: FromDisplay<'a>,
#[label(privacy::visibility_label)] #[label(privacy::visibility_label)]
pub vis_span: Span, pub vis_span: Span,
} }
@ -78,7 +81,7 @@ pub struct InPublicInterface<'a> {
#[lint(privacy::from_private_dep_in_public_interface)] #[lint(privacy::from_private_dep_in_public_interface)]
pub struct FromPrivateDependencyInPublicInterface<'a> { pub struct FromPrivateDependencyInPublicInterface<'a> {
pub kind: &'a str, pub kind: &'a str,
pub descr: String, pub descr: FromDisplay<'a>,
pub krate: Symbol, pub krate: Symbol,
} }
@ -87,5 +90,13 @@ pub struct FromPrivateDependencyInPublicInterface<'a> {
pub struct PrivateInPublicLint<'a> { pub struct PrivateInPublicLint<'a> {
pub vis_descr: &'static str, pub vis_descr: &'static str,
pub kind: &'a str, pub kind: &'a str,
pub descr: String, pub descr: FromDisplay<'a>,
}
pub struct FromDisplay<'a>(pub &'a dyn Display);
impl IntoDiagnosticArg for FromDisplay<'_> {
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
self.0.to_string().into_diagnostic_arg()
}
} }

View file

@ -38,8 +38,9 @@ use std::ops::ControlFlow;
use std::{cmp, fmt, mem}; use std::{cmp, fmt, mem};
use errors::{ use errors::{
FieldIsPrivate, FieldIsPrivateLabel, FromPrivateDependencyInPublicInterface, InPublicInterface, FieldIsPrivate, FieldIsPrivateLabel, FromDisplay, FromPrivateDependencyInPublicInterface,
InPublicInterfaceTraits, ItemIsPrivate, PrivateInPublicLint, UnnamedItemIsPrivate, InPublicInterface, InPublicInterfaceTraits, ItemIsPrivate, PrivateInPublicLint,
UnnamedItemIsPrivate,
}; };
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -1082,7 +1083,7 @@ impl<'tcx> TypePrivacyVisitor<'tcx> {
self.tcx.sess.emit_err(ItemIsPrivate { self.tcx.sess.emit_err(ItemIsPrivate {
span: self.span, span: self.span,
kind, kind,
descr: descr.to_string(), descr: FromDisplay(descr),
}); });
} }
is_error is_error
@ -1255,7 +1256,9 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> {
}; };
let kind = kind.descr(def_id); let kind = kind.descr(def_id);
let _ = match name { let _ = match name {
Some(name) => sess.emit_err(ItemIsPrivate { span, kind, descr: name }), Some(name) => {
sess.emit_err(ItemIsPrivate { span, kind, descr: FromDisplay(&name) })
}
None => sess.emit_err(UnnamedItemIsPrivate { span, kind }), None => sess.emit_err(UnnamedItemIsPrivate { span, kind }),
}; };
return; return;
@ -1723,7 +1726,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
self.tcx.def_span(self.item_def_id.to_def_id()), self.tcx.def_span(self.item_def_id.to_def_id()),
FromPrivateDependencyInPublicInterface { FromPrivateDependencyInPublicInterface {
kind, kind,
descr: descr.to_string(), descr: FromDisplay(descr),
krate: self.tcx.crate_name(def_id.krate), krate: self.tcx.crate_name(def_id.krate),
}, },
); );
@ -1750,7 +1753,6 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
} }
}; };
let span = self.tcx.def_span(self.item_def_id.to_def_id()); let span = self.tcx.def_span(self.item_def_id.to_def_id());
let descr = descr.to_string();
if self.has_old_errors if self.has_old_errors
|| self.in_assoc_ty || self.in_assoc_ty
|| self.tcx.resolutions(()).has_pub_restricted || self.tcx.resolutions(()).has_pub_restricted
@ -1761,7 +1763,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
span, span,
vis_descr, vis_descr,
kind, kind,
descr, descr: FromDisplay(descr),
vis_span, vis_span,
}); });
} else { } else {
@ -1769,7 +1771,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
span, span,
vis_descr, vis_descr,
kind, kind,
descr, descr: FromDisplay(descr),
vis_span, vis_span,
}); });
} }
@ -1778,7 +1780,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
lint::builtin::PRIVATE_IN_PUBLIC, lint::builtin::PRIVATE_IN_PUBLIC,
hir_id, hir_id,
span, span,
PrivateInPublicLint { vis_descr, kind, descr }, PrivateInPublicLint { vis_descr, kind, descr: FromDisplay(descr) },
); );
} }
} }

View file

@ -0,0 +1,24 @@
// check-pass
#![feature(type_alias_impl_trait)]
#![allow(private_in_public)]
pub type Successors<'a> = impl Iterator<Item = &'a ()>;
pub fn f<'a>() -> Successors<'a> {
None.into_iter()
}
trait Tr {
type Item;
}
impl<'a> Tr for &'a () {
type Item = Successors<'a>;
}
pub fn ohno<'a>() -> <&'a () as Tr>::Item {
None.into_iter()
}
fn main() {}