Skip to content

Commit ca5a6e4

Browse files
committed
use proper spans
1 parent c00ff9c commit ca5a6e4

File tree

7 files changed

+54
-28
lines changed

7 files changed

+54
-28
lines changed

compiler/rustc_error_messages/locales/en-US/hir_analysis.ftl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,6 @@ hir_analysis_const_impl_for_non_const_trait =
147147
hir_analysis_const_bound_for_non_const_trait =
148148
~const can only be applied to `#[const_trait]` traits
149149
150-
hir_analysis_self_in_impl_self = `Self` is not valid at this location
150+
hir_analysis_self_in_impl_self =
151+
`Self` is not valid in the self type of an impl block
152+
.note = replace `Self` with a different type

compiler/rustc_hir_analysis/src/collect/type_of.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
322322
ItemKind::Impl(
323323
hir::Impl { self_ty, .. }
324324
) => {
325-
struct MyVisitor(bool);
325+
struct MyVisitor(Vec<Span>);
326326
impl<'v> hir::intravisit::Visitor<'v> for MyVisitor {
327327
fn visit_ty(&mut self, t: &'v Ty<'v>) {
328328
if matches!(
@@ -335,19 +335,22 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
335335
},
336336
))
337337
) {
338-
self.0 = true;
338+
self.0.push(t.span);
339339
return;
340340
}
341341
hir::intravisit::walk_ty(self, t);
342342
}
343343
}
344344

345-
let mut my_visitor = MyVisitor(false);
345+
let mut my_visitor = MyVisitor(vec![]);
346346
my_visitor.visit_ty(self_ty);
347347

348348
match my_visitor.0 {
349-
true => { tcx.sess.emit_err(crate::errors::SelfInImplSelf { span: self_ty.span}); tcx.ty_error() },
350-
false => icx.to_ty(*self_ty),
349+
spans if spans.len() > 0 => {
350+
tcx.sess.emit_err(crate::errors::SelfInImplSelf { span: spans.into(), note: (), });
351+
tcx.ty_error()
352+
},
353+
_ => icx.to_ty(*self_ty),
351354
}
352355
},
353356
ItemKind::Fn(..) => {

compiler/rustc_hir_analysis/src/errors.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Errors emitted by `rustc_hir_analysis`.
22
3-
use rustc_errors::IntoDiagnostic;
43
use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed, Handler};
4+
use rustc_errors::{IntoDiagnostic, MultiSpan};
55
use rustc_macros::{Diagnostic, LintDiagnostic};
66
use rustc_middle::ty::Ty;
77
use rustc_span::{symbol::Ident, Span, Symbol};
@@ -275,5 +275,7 @@ pub struct ConstBoundForNonConstTrait {
275275
#[diag(hir_analysis_self_in_impl_self)]
276276
pub struct SelfInImplSelf {
277277
#[primary_span]
278-
pub span: Span,
278+
pub span: MultiSpan,
279+
#[note]
280+
pub note: (),
279281
}

src/test/ui/resolve/issue-23305.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ pub trait ToNbt<T> {
33
}
44

55
impl dyn ToNbt<Self> {}
6-
//~^ ERROR `Self` is not valid at this location
6+
//~^ ERROR `Self` is not valid in the self type of an impl block
77

88
fn main() {}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
error: `Self` is not valid at this location
2-
--> $DIR/issue-23305.rs:5:6
1+
error: `Self` is not valid in the self type of an impl block
2+
--> $DIR/issue-23305.rs:5:16
33
|
44
LL | impl dyn ToNbt<Self> {}
5-
| ^^^^^^^^^^^^^^^
5+
| ^^^^
6+
|
7+
= note: replace `Self` with a different type
68

79
error: aborting due to previous error
810

src/test/ui/resolve/resolve-self-in-impl.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ impl Tr for S where Self: Copy {} // OK
1111
impl Tr for S where S<Self>: Copy {} // OK
1212
impl Tr for S where Self::A: Copy {} // OK
1313

14-
impl Tr for Self {} //~ ERROR `Self` is not valid at this location
15-
impl Tr for S<Self> {} //~ ERROR `Self` is not valid at this location
16-
impl Self {} //~ ERROR `Self` is not valid at this location
17-
impl S<Self> {} //~ ERROR `Self` is not valid at this location
14+
impl Tr for Self {} //~ ERROR `Self` is not valid in the self type of an impl block
15+
impl Tr for S<Self> {} //~ ERROR `Self` is not valid in the self type of an impl block
16+
impl Self {} //~ ERROR `Self` is not valid in the self type of an impl block
17+
impl S<Self> {} //~ ERROR `Self` is not valid in the self type of an impl block
18+
impl (Self, Self) {} //~ ERROR `Self` is not valid in the self type of an impl block
1819
impl Tr<Self::A> for S {} //~ ERROR cycle detected
1920

2021
fn main() {}
Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,50 @@
1-
error: `Self` is not valid at this location
1+
error: `Self` is not valid in the self type of an impl block
22
--> $DIR/resolve-self-in-impl.rs:14:13
33
|
44
LL | impl Tr for Self {}
55
| ^^^^
6+
|
7+
= note: replace `Self` with a different type
68

7-
error: `Self` is not valid at this location
8-
--> $DIR/resolve-self-in-impl.rs:15:13
9+
error: `Self` is not valid in the self type of an impl block
10+
--> $DIR/resolve-self-in-impl.rs:15:15
911
|
1012
LL | impl Tr for S<Self> {}
11-
| ^^^^^^^
13+
| ^^^^
14+
|
15+
= note: replace `Self` with a different type
1216

13-
error: `Self` is not valid at this location
17+
error: `Self` is not valid in the self type of an impl block
1418
--> $DIR/resolve-self-in-impl.rs:16:6
1519
|
1620
LL | impl Self {}
1721
| ^^^^
22+
|
23+
= note: replace `Self` with a different type
1824

19-
error: `Self` is not valid at this location
20-
--> $DIR/resolve-self-in-impl.rs:17:6
25+
error: `Self` is not valid in the self type of an impl block
26+
--> $DIR/resolve-self-in-impl.rs:17:8
2127
|
2228
LL | impl S<Self> {}
23-
| ^^^^^^^
29+
| ^^^^
30+
|
31+
= note: replace `Self` with a different type
32+
33+
error: `Self` is not valid in the self type of an impl block
34+
--> $DIR/resolve-self-in-impl.rs:18:7
35+
|
36+
LL | impl (Self, Self) {}
37+
| ^^^^ ^^^^
38+
|
39+
= note: replace `Self` with a different type
2440

25-
error[E0391]: cycle detected when computing trait implemented by `<impl at $DIR/resolve-self-in-impl.rs:18:1: 18:23>`
26-
--> $DIR/resolve-self-in-impl.rs:18:1
41+
error[E0391]: cycle detected when computing trait implemented by `<impl at $DIR/resolve-self-in-impl.rs:19:1: 19:23>`
42+
--> $DIR/resolve-self-in-impl.rs:19:1
2743
|
2844
LL | impl Tr<Self::A> for S {}
2945
| ^^^^^^^^^^^^^^^^^^^^^^
3046
|
31-
= note: ...which immediately requires computing trait implemented by `<impl at $DIR/resolve-self-in-impl.rs:18:1: 18:23>` again
47+
= note: ...which immediately requires computing trait implemented by `<impl at $DIR/resolve-self-in-impl.rs:19:1: 19:23>` again
3248
note: cycle used when collecting item types in top-level module
3349
--> $DIR/resolve-self-in-impl.rs:1:1
3450
|
@@ -41,6 +57,6 @@ LL | |
4157
LL | | fn main() {}
4258
| |____________^
4359

44-
error: aborting due to 5 previous errors
60+
error: aborting due to 6 previous errors
4561

4662
For more information about this error, try `rustc --explain E0391`.

0 commit comments

Comments
 (0)