1
Fork 0

Auto merge of #87225 - estebank:cleanup, r=oli-obk

Various diagnostics clean ups/tweaks

* Always point at macros, including derive macros
* Point at non-local items that introduce a trait requirement
* On private associated item, point at definition
This commit is contained in:
bors 2021-07-19 18:44:27 +00:00
commit d5af63480f
163 changed files with 1474 additions and 398 deletions

View file

@ -84,8 +84,10 @@ fn report_bad_target(sess: &Session, item: &Annotatable, span: Span) -> bool {
sess,
span,
E0774,
"`derive` may only be applied to structs, enums and unions",
"`derive` may only be applied to `struct`s, `enum`s and `union`s",
)
.span_label(span, "not applicable here")
.span_label(item.span(), "not a `struct`, `enum` or `union`")
.emit();
}
bad_target
@ -99,6 +101,7 @@ fn report_unexpected_literal(sess: &Session, lit: &ast::Lit) {
_ => "for example, write `#[derive(Debug)]` for `Debug`".to_string(),
};
struct_span_err!(sess, lit.span, E0777, "expected path to a trait, found literal",)
.span_label(lit.span, "not a trait")
.help(&help_msg)
.emit();
}

View file

@ -149,11 +149,7 @@ fn cs_clone_shallow(
}
_ => cx.span_bug(
trait_span,
&format!(
"unexpected substructure in \
shallow `derive({})`",
name
),
&format!("unexpected substructure in shallow `derive({})`", name),
),
}
}

View file

@ -365,10 +365,7 @@ pub trait Emitter {
continue;
}
if matches!(trace.kind, ExpnKind::Inlined) {
new_labels
.push((trace.call_site, "in the inlined copy of this code".to_string()));
} else if always_backtrace {
if always_backtrace && !matches!(trace.kind, ExpnKind::Inlined) {
new_labels.push((
trace.def_site,
format!(
@ -398,13 +395,27 @@ pub trait Emitter {
// and it needs an "in this macro invocation" label to match that.
let redundant_span = trace.call_site.contains(sp);
if !redundant_span && matches!(trace.kind, ExpnKind::Macro(MacroKind::Bang, _))
|| always_backtrace
{
if !redundant_span || always_backtrace {
let msg: Cow<'static, _> = match trace.kind {
ExpnKind::Macro(MacroKind::Attr, _) => {
"this procedural macro expansion".into()
}
ExpnKind::Macro(MacroKind::Derive, _) => {
"this derive macro expansion".into()
}
ExpnKind::Macro(MacroKind::Bang, _) => "this macro invocation".into(),
ExpnKind::Inlined => "the inlined copy of this code".into(),
ExpnKind::Root => "in the crate root".into(),
ExpnKind::AstPass(kind) => kind.descr().into(),
ExpnKind::Desugaring(kind) => {
format!("this {} desugaring", kind.descr()).into()
}
};
new_labels.push((
trace.call_site,
format!(
"in this macro invocation{}",
"in {}{}",
msg,
if macro_backtrace.len() > 1 && always_backtrace {
// only specify order when the macro
// backtrace is multiple levels deep

View file

@ -1071,7 +1071,7 @@ pub enum AstPass {
}
impl AstPass {
fn descr(self) -> &'static str {
pub fn descr(self) -> &'static str {
match self {
AstPass::StdImports => "standard library imports",
AstPass::TestHarness => "test harness",
@ -1108,7 +1108,7 @@ pub enum ForLoopLoc {
impl DesugaringKind {
/// The description wording should combine well with "desugaring of {}".
fn descr(self) -> &'static str {
pub fn descr(self) -> &'static str {
match self {
DesugaringKind::CondTemporary => "`if` or `while` condition",
DesugaringKind::Async => "`async` block or function",

View file

@ -1928,12 +1928,12 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
ObligationCauseCode::ItemObligation(item_def_id) => {
let item_name = tcx.def_path_str(item_def_id);
let msg = format!("required by `{}`", item_name);
if let Some(sp) = tcx.hir().span_if_local(item_def_id) {
let sp = tcx.sess.source_map().guess_head_span(sp);
err.span_label(sp, &msg);
} else {
err.note(&msg);
}
let sp = tcx
.hir()
.span_if_local(item_def_id)
.unwrap_or_else(|| tcx.def_span(item_def_id));
let sp = tcx.sess.source_map().guess_head_span(sp);
err.span_note(sp, &msg);
}
ObligationCauseCode::BindingObligation(item_def_id, span) => {
let item_name = tcx.def_path_str(item_def_id);
@ -1952,7 +1952,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
if span != DUMMY_SP {
err.span_label(span, &msg);
} else {
err.note(&msg);
err.span_note(
tcx.def_span(item_def_id),
&format!("required by a bound in `{}`", item_name),
);
}
}
ObligationCauseCode::ObjectCastObligation(object_ty) => {
@ -1979,9 +1982,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
if self.tcx.sess.is_nightly_build() && is_const_fn {
err.help(
"create an inline `const` block, see RFC \
#2920 <https://github.com/rust-lang/rfcs/pull/2920> \
for more information",
"create an inline `const` block, see RFC #2920 \
<https://github.com/rust-lang/rfcs/pull/2920> for more information",
);
}
}
@ -2168,8 +2170,14 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
self.tcx.for_each_relevant_impl(
parent_def_id,
parent_trait_ref.self_ty().skip_binder(),
|impl_def_id| {
candidates.push(impl_def_id);
|impl_def_id| match self.tcx.hir().get_if_local(impl_def_id) {
Some(Node::Item(hir::Item {
kind: hir::ItemKind::Impl(hir::Impl { .. }),
..
})) => {
candidates.push(impl_def_id);
}
_ => {}
},
);
match &candidates[..] {

View file

@ -933,6 +933,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
item_name
);
err.span_label(item_name.span, &format!("private {}", kind));
let sp = self
.tcx
.hir()
.span_if_local(def_id)
.unwrap_or_else(|| self.tcx.def_span(def_id));
err.span_label(sp, &format!("private {} defined here", kind));
self.suggest_valid_traits(&mut err, out_of_scope_traits);
err.emit();
}