Skip to content

Commit

Permalink
Auto merge of rust-lang#99893 - compiler-errors:issue-99387, r=davidtwco
Browse files Browse the repository at this point in the history
Delay formatting trimmed path until lint/error is emitted

Fixes rust-lang#99387

r? `@davidtwco`
  • Loading branch information
bors authored and ehuss committed Aug 31, 2022
1 parent 3b1c95d commit b4b39cd
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 18 deletions.
20 changes: 20 additions & 0 deletions compiler/rustc_errors/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ pub trait IntoDiagnosticArg {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static>;
}

pub struct DiagnosticArgFromDisplay<'a>(pub &'a dyn fmt::Display);

impl IntoDiagnosticArg for DiagnosticArgFromDisplay<'_> {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
self.0.to_string().into_diagnostic_arg()
}
}

impl<'a> From<&'a dyn fmt::Display> for DiagnosticArgFromDisplay<'a> {
fn from(t: &'a dyn fmt::Display) -> Self {
DiagnosticArgFromDisplay(t)
}
}

impl<'a, T: fmt::Display> From<&'a T> for DiagnosticArgFromDisplay<'a> {
fn from(t: &'a T) -> Self {
DiagnosticArgFromDisplay(t)
}
}

macro_rules! into_diagnostic_arg_using_display {
($( $ty:ty ),+ $(,)?) => {
$(
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ impl fmt::Display for ExplicitBug {
impl error::Error for ExplicitBug {}

pub use diagnostic::{
AddSubdiagnostic, DecorateLint, Diagnostic, DiagnosticArg, DiagnosticArgValue, DiagnosticId,
DiagnosticStyledString, IntoDiagnosticArg, SubDiagnostic,
AddSubdiagnostic, DecorateLint, Diagnostic, DiagnosticArg, DiagnosticArgFromDisplay,
DiagnosticArgValue, DiagnosticId, DiagnosticStyledString, IntoDiagnosticArg, SubDiagnostic,
};
pub use diagnostic_builder::{DiagnosticBuilder, EmissionGuarantee, LintDiagnosticBuilder};
use std::backtrace::Backtrace;
Expand Down
11 changes: 6 additions & 5 deletions compiler/rustc_privacy/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use rustc_errors::DiagnosticArgFromDisplay;
use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic};
use rustc_span::{Span, Symbol};

Expand Down Expand Up @@ -35,7 +36,7 @@ pub struct ItemIsPrivate<'a> {
#[label]
pub span: Span,
pub kind: &'a str,
pub descr: String,
pub descr: DiagnosticArgFromDisplay<'a>,
}

#[derive(SessionDiagnostic)]
Expand All @@ -55,7 +56,7 @@ pub struct InPublicInterfaceTraits<'a> {
pub span: Span,
pub vis_descr: &'static str,
pub kind: &'a str,
pub descr: String,
pub descr: DiagnosticArgFromDisplay<'a>,
#[label(privacy::visibility_label)]
pub vis_span: Span,
}
Expand All @@ -69,7 +70,7 @@ pub struct InPublicInterface<'a> {
pub span: Span,
pub vis_descr: &'static str,
pub kind: &'a str,
pub descr: String,
pub descr: DiagnosticArgFromDisplay<'a>,
#[label(privacy::visibility_label)]
pub vis_span: Span,
}
Expand All @@ -78,7 +79,7 @@ pub struct InPublicInterface<'a> {
#[lint(privacy::from_private_dep_in_public_interface)]
pub struct FromPrivateDependencyInPublicInterface<'a> {
pub kind: &'a str,
pub descr: String,
pub descr: DiagnosticArgFromDisplay<'a>,
pub krate: Symbol,
}

Expand All @@ -87,5 +88,5 @@ pub struct FromPrivateDependencyInPublicInterface<'a> {
pub struct PrivateInPublicLint<'a> {
pub vis_descr: &'static str,
pub kind: &'a str,
pub descr: String,
pub descr: DiagnosticArgFromDisplay<'a>,
}
19 changes: 8 additions & 11 deletions compiler/rustc_privacy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,11 +1079,7 @@ impl<'tcx> TypePrivacyVisitor<'tcx> {
fn check_def_id(&mut self, def_id: DefId, kind: &str, descr: &dyn fmt::Display) -> bool {
let is_error = !self.item_is_accessible(def_id);
if is_error {
self.tcx.sess.emit_err(ItemIsPrivate {
span: self.span,
kind,
descr: descr.to_string(),
});
self.tcx.sess.emit_err(ItemIsPrivate { span: self.span, kind, descr: descr.into() });
}
is_error
}
Expand Down Expand Up @@ -1255,7 +1251,9 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> {
};
let kind = kind.descr(def_id);
let _ = match name {
Some(name) => sess.emit_err(ItemIsPrivate { span, kind, descr: name }),
Some(name) => {
sess.emit_err(ItemIsPrivate { span, kind, descr: (&name).into() })
}
None => sess.emit_err(UnnamedItemIsPrivate { span, kind }),
};
return;
Expand Down Expand Up @@ -1723,7 +1721,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
self.tcx.def_span(self.item_def_id.to_def_id()),
FromPrivateDependencyInPublicInterface {
kind,
descr: descr.to_string(),
descr: descr.into(),
krate: self.tcx.crate_name(def_id.krate),
},
);
Expand All @@ -1750,7 +1748,6 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
}
};
let span = self.tcx.def_span(self.item_def_id.to_def_id());
let descr = descr.to_string();
if self.has_old_errors
|| self.in_assoc_ty
|| self.tcx.resolutions(()).has_pub_restricted
Expand All @@ -1761,15 +1758,15 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
span,
vis_descr,
kind,
descr,
descr: descr.into(),
vis_span,
});
} else {
self.tcx.sess.emit_err(InPublicInterface {
span,
vis_descr,
kind,
descr,
descr: descr.into(),
vis_span,
});
}
Expand All @@ -1778,7 +1775,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
lint::builtin::PRIVATE_IN_PUBLIC,
hir_id,
span,
PrivateInPublicLint { vis_descr, kind, descr },
PrivateInPublicLint { vis_descr, kind, descr: descr.into() },
);
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/test/ui/lint/issue-99387.rs
Original file line number Diff line number Diff line change
@@ -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() {}

0 comments on commit b4b39cd

Please sign in to comment.