use proper spans

This commit is contained in:
Boxy 2022-10-27 22:18:26 +01:00
parent c00ff9c4d0
commit ca5a6e43dd
7 changed files with 54 additions and 28 deletions

View File

@ -147,4 +147,6 @@ hir_analysis_const_impl_for_non_const_trait =
hir_analysis_const_bound_for_non_const_trait =
~const can only be applied to `#[const_trait]` traits
hir_analysis_self_in_impl_self = `Self` is not valid at this location
hir_analysis_self_in_impl_self =
`Self` is not valid in the self type of an impl block
.note = replace `Self` with a different type

View File

@ -322,7 +322,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
ItemKind::Impl(
hir::Impl { self_ty, .. }
) => {
struct MyVisitor(bool);
struct MyVisitor(Vec<Span>);
impl<'v> hir::intravisit::Visitor<'v> for MyVisitor {
fn visit_ty(&mut self, t: &'v Ty<'v>) {
if matches!(
@ -335,19 +335,22 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
},
))
) {
self.0 = true;
self.0.push(t.span);
return;
}
hir::intravisit::walk_ty(self, t);
}
}
let mut my_visitor = MyVisitor(false);
let mut my_visitor = MyVisitor(vec![]);
my_visitor.visit_ty(self_ty);
match my_visitor.0 {
true => { tcx.sess.emit_err(crate::errors::SelfInImplSelf { span: self_ty.span}); tcx.ty_error() },
false => icx.to_ty(*self_ty),
spans if spans.len() > 0 => {
tcx.sess.emit_err(crate::errors::SelfInImplSelf { span: spans.into(), note: (), });
tcx.ty_error()
},
_ => icx.to_ty(*self_ty),
}
},
ItemKind::Fn(..) => {

View File

@ -1,7 +1,7 @@
//! Errors emitted by `rustc_hir_analysis`.
use rustc_errors::IntoDiagnostic;
use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed, Handler};
use rustc_errors::{IntoDiagnostic, MultiSpan};
use rustc_macros::{Diagnostic, LintDiagnostic};
use rustc_middle::ty::Ty;
use rustc_span::{symbol::Ident, Span, Symbol};
@ -275,5 +275,7 @@ pub struct ConstBoundForNonConstTrait {
#[diag(hir_analysis_self_in_impl_self)]
pub struct SelfInImplSelf {
#[primary_span]
pub span: Span,
pub span: MultiSpan,
#[note]
pub note: (),
}

View File

@ -3,6 +3,6 @@ pub trait ToNbt<T> {
}
impl dyn ToNbt<Self> {}
//~^ ERROR `Self` is not valid at this location
//~^ ERROR `Self` is not valid in the self type of an impl block
fn main() {}

View File

@ -1,8 +1,10 @@
error: `Self` is not valid at this location
--> $DIR/issue-23305.rs:5:6
error: `Self` is not valid in the self type of an impl block
--> $DIR/issue-23305.rs:5:16
|
LL | impl dyn ToNbt<Self> {}
| ^^^^^^^^^^^^^^^
| ^^^^
|
= note: replace `Self` with a different type
error: aborting due to previous error

View File

@ -11,10 +11,11 @@ impl Tr for S where Self: Copy {} // OK
impl Tr for S where S<Self>: Copy {} // OK
impl Tr for S where Self::A: Copy {} // OK
impl Tr for Self {} //~ ERROR `Self` is not valid at this location
impl Tr for S<Self> {} //~ ERROR `Self` is not valid at this location
impl Self {} //~ ERROR `Self` is not valid at this location
impl S<Self> {} //~ ERROR `Self` is not valid at this location
impl Tr for Self {} //~ ERROR `Self` is not valid in the self type of an impl block
impl Tr for S<Self> {} //~ ERROR `Self` is not valid in the self type of an impl block
impl Self {} //~ ERROR `Self` is not valid in the self type of an impl block
impl S<Self> {} //~ ERROR `Self` is not valid in the self type of an impl block
impl (Self, Self) {} //~ ERROR `Self` is not valid in the self type of an impl block
impl Tr<Self::A> for S {} //~ ERROR cycle detected
fn main() {}

View File

@ -1,34 +1,50 @@
error: `Self` is not valid at this location
error: `Self` is not valid in the self type of an impl block
--> $DIR/resolve-self-in-impl.rs:14:13
|
LL | impl Tr for Self {}
| ^^^^
|
= note: replace `Self` with a different type
error: `Self` is not valid at this location
--> $DIR/resolve-self-in-impl.rs:15:13
error: `Self` is not valid in the self type of an impl block
--> $DIR/resolve-self-in-impl.rs:15:15
|
LL | impl Tr for S<Self> {}
| ^^^^^^^
| ^^^^
|
= note: replace `Self` with a different type
error: `Self` is not valid at this location
error: `Self` is not valid in the self type of an impl block
--> $DIR/resolve-self-in-impl.rs:16:6
|
LL | impl Self {}
| ^^^^
|
= note: replace `Self` with a different type
error: `Self` is not valid at this location
--> $DIR/resolve-self-in-impl.rs:17:6
error: `Self` is not valid in the self type of an impl block
--> $DIR/resolve-self-in-impl.rs:17:8
|
LL | impl S<Self> {}
| ^^^^^^^
| ^^^^
|
= note: replace `Self` with a different type
error[E0391]: cycle detected when computing trait implemented by `<impl at $DIR/resolve-self-in-impl.rs:18:1: 18:23>`
--> $DIR/resolve-self-in-impl.rs:18:1
error: `Self` is not valid in the self type of an impl block
--> $DIR/resolve-self-in-impl.rs:18:7
|
LL | impl (Self, Self) {}
| ^^^^ ^^^^
|
= note: replace `Self` with a different type
error[E0391]: cycle detected when computing trait implemented by `<impl at $DIR/resolve-self-in-impl.rs:19:1: 19:23>`
--> $DIR/resolve-self-in-impl.rs:19:1
|
LL | impl Tr<Self::A> for S {}
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: ...which immediately requires computing trait implemented by `<impl at $DIR/resolve-self-in-impl.rs:18:1: 18:23>` again
= note: ...which immediately requires computing trait implemented by `<impl at $DIR/resolve-self-in-impl.rs:19:1: 19:23>` again
note: cycle used when collecting item types in top-level module
--> $DIR/resolve-self-in-impl.rs:1:1
|
@ -41,6 +57,6 @@ LL | |
LL | | fn main() {}
| |____________^
error: aborting due to 5 previous errors
error: aborting due to 6 previous errors
For more information about this error, try `rustc --explain E0391`.