Skip to content

Commit d5af634

Browse files
committed
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
2 parents fad295b + ba052bd commit d5af634

File tree

163 files changed

+1474
-398
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

163 files changed

+1474
-398
lines changed

Diff for: compiler/rustc_builtin_macros/src/derive.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,10 @@ fn report_bad_target(sess: &Session, item: &Annotatable, span: Span) -> bool {
8484
sess,
8585
span,
8686
E0774,
87-
"`derive` may only be applied to structs, enums and unions",
87+
"`derive` may only be applied to `struct`s, `enum`s and `union`s",
8888
)
89+
.span_label(span, "not applicable here")
90+
.span_label(item.span(), "not a `struct`, `enum` or `union`")
8991
.emit();
9092
}
9193
bad_target
@@ -99,6 +101,7 @@ fn report_unexpected_literal(sess: &Session, lit: &ast::Lit) {
99101
_ => "for example, write `#[derive(Debug)]` for `Debug`".to_string(),
100102
};
101103
struct_span_err!(sess, lit.span, E0777, "expected path to a trait, found literal",)
104+
.span_label(lit.span, "not a trait")
102105
.help(&help_msg)
103106
.emit();
104107
}

Diff for: compiler/rustc_builtin_macros/src/deriving/clone.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,7 @@ fn cs_clone_shallow(
149149
}
150150
_ => cx.span_bug(
151151
trait_span,
152-
&format!(
153-
"unexpected substructure in \
154-
shallow `derive({})`",
155-
name
156-
),
152+
&format!("unexpected substructure in shallow `derive({})`", name),
157153
),
158154
}
159155
}

Diff for: compiler/rustc_errors/src/emitter.rs

+19-8
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,7 @@ pub trait Emitter {
365365
continue;
366366
}
367367

368-
if matches!(trace.kind, ExpnKind::Inlined) {
369-
new_labels
370-
.push((trace.call_site, "in the inlined copy of this code".to_string()));
371-
} else if always_backtrace {
368+
if always_backtrace && !matches!(trace.kind, ExpnKind::Inlined) {
372369
new_labels.push((
373370
trace.def_site,
374371
format!(
@@ -398,13 +395,27 @@ pub trait Emitter {
398395
// and it needs an "in this macro invocation" label to match that.
399396
let redundant_span = trace.call_site.contains(sp);
400397

401-
if !redundant_span && matches!(trace.kind, ExpnKind::Macro(MacroKind::Bang, _))
402-
|| always_backtrace
403-
{
398+
if !redundant_span || always_backtrace {
399+
let msg: Cow<'static, _> = match trace.kind {
400+
ExpnKind::Macro(MacroKind::Attr, _) => {
401+
"this procedural macro expansion".into()
402+
}
403+
ExpnKind::Macro(MacroKind::Derive, _) => {
404+
"this derive macro expansion".into()
405+
}
406+
ExpnKind::Macro(MacroKind::Bang, _) => "this macro invocation".into(),
407+
ExpnKind::Inlined => "the inlined copy of this code".into(),
408+
ExpnKind::Root => "in the crate root".into(),
409+
ExpnKind::AstPass(kind) => kind.descr().into(),
410+
ExpnKind::Desugaring(kind) => {
411+
format!("this {} desugaring", kind.descr()).into()
412+
}
413+
};
404414
new_labels.push((
405415
trace.call_site,
406416
format!(
407-
"in this macro invocation{}",
417+
"in {}{}",
418+
msg,
408419
if macro_backtrace.len() > 1 && always_backtrace {
409420
// only specify order when the macro
410421
// backtrace is multiple levels deep

Diff for: compiler/rustc_span/src/hygiene.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,7 @@ pub enum AstPass {
10711071
}
10721072

10731073
impl AstPass {
1074-
fn descr(self) -> &'static str {
1074+
pub fn descr(self) -> &'static str {
10751075
match self {
10761076
AstPass::StdImports => "standard library imports",
10771077
AstPass::TestHarness => "test harness",
@@ -1108,7 +1108,7 @@ pub enum ForLoopLoc {
11081108

11091109
impl DesugaringKind {
11101110
/// The description wording should combine well with "desugaring of {}".
1111-
fn descr(self) -> &'static str {
1111+
pub fn descr(self) -> &'static str {
11121112
match self {
11131113
DesugaringKind::CondTemporary => "`if` or `while` condition",
11141114
DesugaringKind::Async => "`async` block or function",

Diff for: compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+20-12
Original file line numberDiff line numberDiff line change
@@ -1928,12 +1928,12 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
19281928
ObligationCauseCode::ItemObligation(item_def_id) => {
19291929
let item_name = tcx.def_path_str(item_def_id);
19301930
let msg = format!("required by `{}`", item_name);
1931-
if let Some(sp) = tcx.hir().span_if_local(item_def_id) {
1932-
let sp = tcx.sess.source_map().guess_head_span(sp);
1933-
err.span_label(sp, &msg);
1934-
} else {
1935-
err.note(&msg);
1936-
}
1931+
let sp = tcx
1932+
.hir()
1933+
.span_if_local(item_def_id)
1934+
.unwrap_or_else(|| tcx.def_span(item_def_id));
1935+
let sp = tcx.sess.source_map().guess_head_span(sp);
1936+
err.span_note(sp, &msg);
19371937
}
19381938
ObligationCauseCode::BindingObligation(item_def_id, span) => {
19391939
let item_name = tcx.def_path_str(item_def_id);
@@ -1952,7 +1952,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
19521952
if span != DUMMY_SP {
19531953
err.span_label(span, &msg);
19541954
} else {
1955-
err.note(&msg);
1955+
err.span_note(
1956+
tcx.def_span(item_def_id),
1957+
&format!("required by a bound in `{}`", item_name),
1958+
);
19561959
}
19571960
}
19581961
ObligationCauseCode::ObjectCastObligation(object_ty) => {
@@ -1979,9 +1982,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
19791982

19801983
if self.tcx.sess.is_nightly_build() && is_const_fn {
19811984
err.help(
1982-
"create an inline `const` block, see RFC \
1983-
#2920 <https://github.com/rust-lang/rfcs/pull/2920> \
1984-
for more information",
1985+
"create an inline `const` block, see RFC #2920 \
1986+
<https://github.com/rust-lang/rfcs/pull/2920> for more information",
19851987
);
19861988
}
19871989
}
@@ -2168,8 +2170,14 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
21682170
self.tcx.for_each_relevant_impl(
21692171
parent_def_id,
21702172
parent_trait_ref.self_ty().skip_binder(),
2171-
|impl_def_id| {
2172-
candidates.push(impl_def_id);
2173+
|impl_def_id| match self.tcx.hir().get_if_local(impl_def_id) {
2174+
Some(Node::Item(hir::Item {
2175+
kind: hir::ItemKind::Impl(hir::Impl { .. }),
2176+
..
2177+
})) => {
2178+
candidates.push(impl_def_id);
2179+
}
2180+
_ => {}
21732181
},
21742182
);
21752183
match &candidates[..] {

Diff for: compiler/rustc_typeck/src/check/method/suggest.rs

+6
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
933933
item_name
934934
);
935935
err.span_label(item_name.span, &format!("private {}", kind));
936+
let sp = self
937+
.tcx
938+
.hir()
939+
.span_if_local(def_id)
940+
.unwrap_or_else(|| self.tcx.def_span(def_id));
941+
err.span_label(sp, &format!("private {} defined here", kind));
936942
self.suggest_valid_traits(&mut err, out_of_scope_traits);
937943
err.emit();
938944
}

Diff for: src/test/ui-fulldeps/session-derive-errors.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ LL | #[message = "This error has a field, and references {name}"]
5656
error: invalid format string: expected `'}'` but string was terminated
5757
--> $DIR/session-derive-errors.rs:116:1
5858
|
59+
LL | #[derive(SessionDiagnostic)]
60+
| ----------------- in this derive macro expansion
5961
LL | #[error = "E0123"]
6062
| - because of this opening brace
6163
LL | #[message = "This is missing a closing brace: {name"]
@@ -67,6 +69,9 @@ LL | #[message = "This is missing a closing brace: {name"]
6769
error: invalid format string: unmatched `}` found
6870
--> $DIR/session-derive-errors.rs:125:1
6971
|
72+
LL | #[derive(SessionDiagnostic)]
73+
| ----------------- in this derive macro expansion
74+
LL | #[error = "E0123"]
7075
LL | #[message = "This is missing an opening brace: name}"]
7176
| ^ unmatched `}` in format string
7277
|

Diff for: src/test/ui/allocator/not-an-allocator.stderr

+28-4
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,61 @@
11
error[E0277]: the trait bound `usize: GlobalAlloc` is not satisfied
22
--> $DIR/not-an-allocator.rs:2:1
33
|
4+
LL | #[global_allocator]
5+
| ------------------- in this procedural macro expansion
46
LL | static A: usize = 0;
57
| ^^^^^^^^^^^^^^^^^^^^ the trait `GlobalAlloc` is not implemented for `usize`
68
|
7-
= note: required by `std::alloc::GlobalAlloc::alloc`
9+
note: required by `std::alloc::GlobalAlloc::alloc`
10+
--> $SRC_DIR/core/src/alloc/global.rs:LL:COL
11+
|
12+
LL | unsafe fn alloc(&self, layout: Layout) -> *mut u8;
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
814
= note: this error originates in the attribute macro `global_allocator` (in Nightly builds, run with -Z macro-backtrace for more info)
915

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

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

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

3761
error: aborting due to 4 previous errors

Diff for: src/test/ui/allocator/two-allocators.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: cannot define multiple global allocators
44
LL | static A: System = System;
55
| -------------------------- previous global allocator defined here
66
LL | #[global_allocator]
7+
| ------------------- in this procedural macro expansion
78
LL | static B: System = System;
89
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot define a new global allocator
910
|

Diff for: src/test/ui/associated-consts/associated-const-array-len.stderr

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
error[E0277]: the trait bound `i32: Foo` is not satisfied
22
--> $DIR/associated-const-array-len.rs:5:16
33
|
4-
LL | const ID: usize;
5-
| ---------------- required by `Foo::ID`
6-
...
74
LL | const X: [i32; <i32 as Foo>::ID] = [0, 1, 2];
85
| ^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `i32`
6+
|
7+
note: required by `Foo::ID`
8+
--> $DIR/associated-const-array-len.rs:2:5
9+
|
10+
LL | const ID: usize;
11+
| ^^^^^^^^^^^^^^^^
912

1013
error: aborting due to previous error
1114

Diff for: src/test/ui/associated-consts/associated-const-private-impl.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error[E0624]: associated constant `ID` is private
22
--> $DIR/associated-const-private-impl.rs:13:30
33
|
4+
LL | const ID: i32 = 1;
5+
| ------------------ private associated constant defined here
6+
...
47
LL | assert_eq!(1, bar1::Foo::ID);
58
| ^^ private associated constant
69

Diff for: src/test/ui/associated-consts/issue-63496.stderr

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
error[E0283]: type annotations needed
22
--> $DIR/issue-63496.rs:4:21
33
|
4-
LL | const C: usize;
5-
| --------------- required by `A::C`
6-
LL |
74
LL | fn f() -> ([u8; A::C], [u8; A::C]);
85
| ^^^^
96
| |
@@ -12,13 +9,15 @@ LL | fn f() -> ([u8; A::C], [u8; A::C]);
129
|
1310
= note: cannot satisfy `_: A`
1411
= note: associated constants cannot be accessed directly on a `trait`, they can only be accessed through a specific `impl`
12+
note: required by `A::C`
13+
--> $DIR/issue-63496.rs:2:5
14+
|
15+
LL | const C: usize;
16+
| ^^^^^^^^^^^^^^^
1517

1618
error[E0283]: type annotations needed
1719
--> $DIR/issue-63496.rs:4:33
1820
|
19-
LL | const C: usize;
20-
| --------------- required by `A::C`
21-
LL |
2221
LL | fn f() -> ([u8; A::C], [u8; A::C]);
2322
| ^^^^
2423
| |
@@ -27,6 +26,11 @@ LL | fn f() -> ([u8; A::C], [u8; A::C]);
2726
|
2827
= note: cannot satisfy `_: A`
2928
= note: associated constants cannot be accessed directly on a `trait`, they can only be accessed through a specific `impl`
29+
note: required by `A::C`
30+
--> $DIR/issue-63496.rs:2:5
31+
|
32+
LL | const C: usize;
33+
| ^^^^^^^^^^^^^^^
3034

3135
error: aborting due to 2 previous errors
3236

Diff for: src/test/ui/associated-item/issue-48027.stderr

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ LL | const X: usize;
1616
error[E0283]: type annotations needed
1717
--> $DIR/issue-48027.rs:3:32
1818
|
19-
LL | const X: usize;
20-
| --------------- required by `Bar::X`
2119
LL | fn return_n(&self) -> [u8; Bar::X];
2220
| ^^^^^^
2321
| |
@@ -26,6 +24,11 @@ LL | fn return_n(&self) -> [u8; Bar::X];
2624
|
2725
= note: cannot satisfy `_: Bar`
2826
= note: associated constants cannot be accessed directly on a `trait`, they can only be accessed through a specific `impl`
27+
note: required by `Bar::X`
28+
--> $DIR/issue-48027.rs:2:5
29+
|
30+
LL | const X: usize;
31+
| ^^^^^^^^^^^^^^^
2932

3033
error: aborting due to 2 previous errors
3134

Diff for: src/test/ui/associated-types/associated-types-bound-failure.stderr

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
error[E0277]: the trait bound `<G as GetToInt>::R: ToInt` is not satisfied
22
--> $DIR/associated-types-bound-failure.rs:19:19
33
|
4-
LL | fn to_int(&self) -> isize;
5-
| -------------------------- required by `ToInt::to_int`
6-
...
74
LL | ToInt::to_int(&g.get())
85
| ^^^^^^^^ the trait `ToInt` is not implemented for `<G as GetToInt>::R`
96
|
7+
note: required by `ToInt::to_int`
8+
--> $DIR/associated-types-bound-failure.rs:6:5
9+
|
10+
LL | fn to_int(&self) -> isize;
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1012
help: consider further restricting the associated type
1113
|
1214
LL | where G : GetToInt, <G as GetToInt>::R: ToInt

Diff for: src/test/ui/associated-types/associated-types-unconstrained.stderr

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
error[E0283]: type annotations needed
22
--> $DIR/associated-types-unconstrained.rs:14:20
33
|
4-
LL | fn bar() -> isize;
5-
| ------------------ required by `Foo::bar`
6-
...
74
LL | let x: isize = Foo::bar();
85
| ^^^^^^^^ cannot infer type
96
|
107
= note: cannot satisfy `_: Foo`
8+
note: required by `Foo::bar`
9+
--> $DIR/associated-types-unconstrained.rs:5:5
10+
|
11+
LL | fn bar() -> isize;
12+
| ^^^^^^^^^^^^^^^^^^
1113

1214
error: aborting due to previous error
1315

0 commit comments

Comments
 (0)