Skip to content

Various diagnostics clean ups/tweaks #87225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion compiler/rustc_builtin_macros/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
Expand Down
6 changes: 1 addition & 5 deletions compiler/rustc_builtin_macros/src/deriving/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,7 @@ fn cs_clone_shallow(
}
_ => cx.span_bug(
trait_span,
&format!(
"unexpected substructure in \
shallow `derive({})`",
name
),
&format!("unexpected substructure in shallow `derive({})`", name),
),
}
}
Expand Down
27 changes: 19 additions & 8 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_span/src/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,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",
Expand Down Expand Up @@ -989,7 +989,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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) => {
Expand All @@ -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",
);
}
}
Expand Down Expand Up @@ -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[..] {
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_typeck/src/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Comment on lines +936 to +940
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this pattern could warrant its own helper function

err.span_label(sp, &format!("private {} defined here", kind));
self.suggest_valid_traits(&mut err, out_of_scope_traits);
err.emit();
}
Expand Down
5 changes: 5 additions & 0 deletions src/test/ui-fulldeps/session-derive-errors.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ LL | #[message = "This error has a field, and references {name}"]
error: invalid format string: expected `'}'` but string was terminated
--> $DIR/session-derive-errors.rs:116:1
|
LL | #[derive(SessionDiagnostic)]
| ----------------- in this derive macro expansion
LL | #[error = "E0123"]
| - because of this opening brace
LL | #[message = "This is missing a closing brace: {name"]
Expand All @@ -67,6 +69,9 @@ LL | #[message = "This is missing a closing brace: {name"]
error: invalid format string: unmatched `}` found
--> $DIR/session-derive-errors.rs:125:1
|
LL | #[derive(SessionDiagnostic)]
| ----------------- in this derive macro expansion
LL | #[error = "E0123"]
LL | #[message = "This is missing an opening brace: name}"]
| ^ unmatched `}` in format string
|
Expand Down
32 changes: 28 additions & 4 deletions src/test/ui/allocator/not-an-allocator.stderr
Original file line number Diff line number Diff line change
@@ -1,37 +1,61 @@
error[E0277]: the trait bound `usize: GlobalAlloc` is not satisfied
--> $DIR/not-an-allocator.rs:2:1
|
LL | #[global_allocator]
| ------------------- in this procedural macro expansion
LL | static A: usize = 0;
| ^^^^^^^^^^^^^^^^^^^^ the trait `GlobalAlloc` is not implemented for `usize`
|
= note: required by `std::alloc::GlobalAlloc::alloc`
note: required by `std::alloc::GlobalAlloc::alloc`
--> $SRC_DIR/core/src/alloc/global.rs:LL:COL
|
LL | unsafe fn alloc(&self, layout: Layout) -> *mut u8;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in the attribute macro `global_allocator` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `usize: GlobalAlloc` is not satisfied
--> $DIR/not-an-allocator.rs:2:1
|
LL | #[global_allocator]
| ------------------- in this procedural macro expansion
LL | static A: usize = 0;
| ^^^^^^^^^^^^^^^^^^^^ the trait `GlobalAlloc` is not implemented for `usize`
|
= note: required by `std::alloc::GlobalAlloc::dealloc`
note: required by `std::alloc::GlobalAlloc::dealloc`
--> $SRC_DIR/core/src/alloc/global.rs:LL:COL
|
LL | unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in the attribute macro `global_allocator` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `usize: GlobalAlloc` is not satisfied
--> $DIR/not-an-allocator.rs:2:1
|
LL | #[global_allocator]
| ------------------- in this procedural macro expansion
LL | static A: usize = 0;
| ^^^^^^^^^^^^^^^^^^^^ the trait `GlobalAlloc` is not implemented for `usize`
|
= note: required by `std::alloc::GlobalAlloc::realloc`
note: required by `std::alloc::GlobalAlloc::realloc`
--> $SRC_DIR/core/src/alloc/global.rs:LL:COL
|
LL | unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in the attribute macro `global_allocator` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `usize: GlobalAlloc` is not satisfied
--> $DIR/not-an-allocator.rs:2:1
|
LL | #[global_allocator]
| ------------------- in this procedural macro expansion
LL | static A: usize = 0;
| ^^^^^^^^^^^^^^^^^^^^ the trait `GlobalAlloc` is not implemented for `usize`
|
= note: required by `std::alloc::GlobalAlloc::alloc_zeroed`
note: required by `std::alloc::GlobalAlloc::alloc_zeroed`
--> $SRC_DIR/core/src/alloc/global.rs:LL:COL
|
LL | unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in the attribute macro `global_allocator` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 4 previous errors
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/allocator/two-allocators.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ error: cannot define multiple global allocators
LL | static A: System = System;
| -------------------------- previous global allocator defined here
LL | #[global_allocator]
| ------------------- in this procedural macro expansion
LL | static B: System = System;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot define a new global allocator
|
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
error[E0277]: the trait bound `i32: Foo` is not satisfied
--> $DIR/associated-const-array-len.rs:5:16
|
LL | const ID: usize;
| ---------------- required by `Foo::ID`
...
LL | const X: [i32; <i32 as Foo>::ID] = [0, 1, 2];
| ^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `i32`
|
note: required by `Foo::ID`
--> $DIR/associated-const-array-len.rs:2:5
|
LL | const ID: usize;
| ^^^^^^^^^^^^^^^^

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
error[E0624]: associated constant `ID` is private
--> $DIR/associated-const-private-impl.rs:13:30
|
LL | const ID: i32 = 1;
| ------------------ private associated constant defined here
...
LL | assert_eq!(1, bar1::Foo::ID);
| ^^ private associated constant

Expand Down
16 changes: 10 additions & 6 deletions src/test/ui/associated-consts/issue-63496.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
error[E0283]: type annotations needed
--> $DIR/issue-63496.rs:4:21
|
LL | const C: usize;
| --------------- required by `A::C`
LL |
LL | fn f() -> ([u8; A::C], [u8; A::C]);
| ^^^^
| |
Expand All @@ -12,13 +9,15 @@ LL | fn f() -> ([u8; A::C], [u8; A::C]);
|
= note: cannot satisfy `_: A`
= note: associated constants cannot be accessed directly on a `trait`, they can only be accessed through a specific `impl`
note: required by `A::C`
--> $DIR/issue-63496.rs:2:5
|
LL | const C: usize;
| ^^^^^^^^^^^^^^^

error[E0283]: type annotations needed
--> $DIR/issue-63496.rs:4:33
|
LL | const C: usize;
| --------------- required by `A::C`
LL |
LL | fn f() -> ([u8; A::C], [u8; A::C]);
| ^^^^
| |
Expand All @@ -27,6 +26,11 @@ LL | fn f() -> ([u8; A::C], [u8; A::C]);
|
= note: cannot satisfy `_: A`
= note: associated constants cannot be accessed directly on a `trait`, they can only be accessed through a specific `impl`
note: required by `A::C`
--> $DIR/issue-63496.rs:2:5
|
LL | const C: usize;
| ^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

Expand Down
7 changes: 5 additions & 2 deletions src/test/ui/associated-item/issue-48027.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ LL | const X: usize;
error[E0283]: type annotations needed
--> $DIR/issue-48027.rs:3:32
|
LL | const X: usize;
| --------------- required by `Bar::X`
LL | fn return_n(&self) -> [u8; Bar::X];
| ^^^^^^
| |
Expand All @@ -26,6 +24,11 @@ LL | fn return_n(&self) -> [u8; Bar::X];
|
= note: cannot satisfy `_: Bar`
= note: associated constants cannot be accessed directly on a `trait`, they can only be accessed through a specific `impl`
note: required by `Bar::X`
--> $DIR/issue-48027.rs:2:5
|
LL | const X: usize;
| ^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
error[E0277]: the trait bound `<G as GetToInt>::R: ToInt` is not satisfied
--> $DIR/associated-types-bound-failure.rs:19:19
|
LL | fn to_int(&self) -> isize;
| -------------------------- required by `ToInt::to_int`
...
LL | ToInt::to_int(&g.get())
| ^^^^^^^^ the trait `ToInt` is not implemented for `<G as GetToInt>::R`
|
note: required by `ToInt::to_int`
--> $DIR/associated-types-bound-failure.rs:6:5
|
LL | fn to_int(&self) -> isize;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider further restricting the associated type
|
LL | where G : GetToInt, <G as GetToInt>::R: ToInt
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
error[E0283]: type annotations needed
--> $DIR/associated-types-unconstrained.rs:14:20
|
LL | fn bar() -> isize;
| ------------------ required by `Foo::bar`
...
LL | let x: isize = Foo::bar();
| ^^^^^^^^ cannot infer type
|
= note: cannot satisfy `_: Foo`
note: required by `Foo::bar`
--> $DIR/associated-types-unconstrained.rs:5:5
|
LL | fn bar() -> isize;
| ^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

Expand Down
Loading