Rollup merge of #103609 - BoxyUwU:fix_impl_self_cycle, r=compiler-errors
Emit a nicer error on `impl Self {` currently it emits a "cycle detected error" but this PR makes it emit a more user friendly error specifically saying that `Self` is disallowed in that position. this is a pretty hacky fix so i dont expect this to be merged (I basically only made this PR because i wanted to see if CI passes) r? ``@compiler-errors``
This commit is contained in:
commit
ab0d9dfefe
8 changed files with 75 additions and 77 deletions
|
@ -146,3 +146,7 @@ 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 in the self type of an impl block
|
||||
.note = replace `Self` with a different type
|
||||
|
|
|
@ -2418,6 +2418,30 @@ impl<'hir> Ty<'hir> {
|
|||
}
|
||||
final_ty
|
||||
}
|
||||
|
||||
pub fn find_self_aliases(&self) -> Vec<Span> {
|
||||
use crate::intravisit::Visitor;
|
||||
struct MyVisitor(Vec<Span>);
|
||||
impl<'v> Visitor<'v> for MyVisitor {
|
||||
fn visit_ty(&mut self, t: &'v Ty<'v>) {
|
||||
if matches!(
|
||||
&t.kind,
|
||||
TyKind::Path(QPath::Resolved(
|
||||
_,
|
||||
Path { res: crate::def::Res::SelfTyAlias { .. }, .. },
|
||||
))
|
||||
) {
|
||||
self.0.push(t.span);
|
||||
return;
|
||||
}
|
||||
crate::intravisit::walk_ty(self, t);
|
||||
}
|
||||
}
|
||||
|
||||
let mut my_visitor = MyVisitor(vec![]);
|
||||
my_visitor.visit_ty(self);
|
||||
my_visitor.0
|
||||
}
|
||||
}
|
||||
|
||||
/// Not represented directly in the AST; referred to by name through a `ty_path`.
|
||||
|
|
|
@ -319,7 +319,15 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
|
|||
}
|
||||
}
|
||||
ItemKind::TyAlias(self_ty, _) => icx.to_ty(self_ty),
|
||||
ItemKind::Impl(hir::Impl { self_ty, .. }) => icx.to_ty(*self_ty),
|
||||
ItemKind::Impl(hir::Impl { self_ty, .. }) => {
|
||||
match self_ty.find_self_aliases() {
|
||||
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(..) => {
|
||||
let substs = InternalSubsts::identity_for_item(tcx, def_id.to_def_id());
|
||||
tcx.mk_fn_def(def_id.to_def_id(), substs)
|
||||
|
|
|
@ -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};
|
||||
|
@ -270,3 +270,12 @@ pub struct ConstBoundForNonConstTrait {
|
|||
#[primary_span]
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag(hir_analysis_self_in_impl_self)]
|
||||
pub struct SelfInImplSelf {
|
||||
#[primary_span]
|
||||
pub span: MultiSpan,
|
||||
#[note]
|
||||
pub note: (),
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue