Skip to content

associated types #1069

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

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
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
130 changes: 61 additions & 69 deletions crates/hir-analysis/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,59 @@ impl DiagnosticVoucher for NameResDiag<'_> {
}
}

Self::MethodNotFound {
primary,
method_name,
receiver,
} => {
let (recv_name, recv_ty, recv_kind) = match receiver {
Either::Left(ty) => (ty.pretty_print(db), Some(ty), ty.kind_name(db)),
Either::Right(trait_) => {
let name = trait_.trait_(db).name(db).unwrap().data(db);
(name, None, "trait".to_string())
}
};

let method_str = method_name.data(db);
let message = format!(
"no method named `{}` found for {} `{}`",
method_str, recv_kind, recv_name
);

if let Some(ty) = recv_ty {
if let Some(field_ty) = ty.record_field_ty(db, *method_name) {
return CompleteDiagnostic {
severity: Severity::Error,
message,
sub_diagnostics: vec![SubDiagnostic {
style: LabelStyle::Primary,
message: format!(
"field `{}` in `{}` has type `{}`",
method_str,
recv_name,
field_ty.pretty_print(db)
),
span: primary.resolve(db),
}],
notes: vec![],
error_code,
};
}
}

CompleteDiagnostic {
severity: Severity::Error,
message,
sub_diagnostics: vec![SubDiagnostic {
style: LabelStyle::Primary,
message: format!("method not found in `{}`", recv_name),
span: primary.resolve(db),
}],
notes: vec![],
error_code,
}
}

Self::Invisible(prim_span, ident, span) => {
let ident = ident.data(db);

Expand Down Expand Up @@ -311,14 +364,18 @@ impl DiagnosticVoucher for NameResDiag<'_> {

Self::TooManyGenericArgs {
span,
ty,
expected,
given,
} => CompleteDiagnostic {
severity: Severity::Error,
message: format!("too many generic args; expected {expected}, given {given}"),
sub_diagnostics: vec![SubDiagnostic {
style: LabelStyle::Primary,
message: format!("expected {expected} arguments here, but {given} given"),
message: format!(
"`{}` expects {expected} arguments, but {given} were given",
ty.pretty_print(db)
),
span: span.resolve(db),
}],
notes: vec![],
Expand Down Expand Up @@ -736,18 +793,6 @@ impl DiagnosticVoucher for TyLowerDiag<'_> {
error_code,
},

Self::AssocTy(span) => CompleteDiagnostic {
severity: Severity::Error,
message: "associated type is not supported ".to_string(),
sub_diagnostics: vec![SubDiagnostic {
style: LabelStyle::Primary,
message: "associated type is not implemented".to_string(),
span: span.resolve(db),
}],
notes: vec![],
error_code,
},

Self::InvalidConstTyExpr(span) => CompleteDiagnostic {
severity: Severity::Error,
message: "the expression is not supported yet in a const type context".to_string(),
Expand Down Expand Up @@ -1437,7 +1482,7 @@ impl DiagnosticVoucher for BodyDiag<'_> {
Self::AmbiguousInherentMethodCall {
primary,
method_name,
cand_spans,
candidates,
} => {
let method_name = method_name.data(db);
let mut sub_diagnostics = vec![SubDiagnostic {
Expand All @@ -1446,11 +1491,11 @@ impl DiagnosticVoucher for BodyDiag<'_> {
span: primary.resolve(db),
}];

for span in cand_spans {
for cand in candidates {
sub_diagnostics.push(SubDiagnostic {
style: LabelStyle::Secondary,
message: format!("`{method_name}` is defined here"),
span: span.resolve(db),
span: cand.name_span(db).resolve(db),
});
}

Expand Down Expand Up @@ -1543,59 +1588,6 @@ impl DiagnosticVoucher for BodyDiag<'_> {
}
}

Self::MethodNotFound {
primary,
method_name,
receiver,
} => {
let (recv_name, recv_ty, recv_kind) = match receiver {
Either::Left(ty) => (ty.pretty_print(db), Some(ty), ty.kind_name(db)),
Either::Right(trait_) => {
let name = trait_.trait_(db).name(db).unwrap().data(db);
(name, None, "trait".to_string())
}
};

let method_str = method_name.data(db);
let message = format!(
"no method named `{}` found for {} `{}`",
method_str, recv_kind, recv_name
);

if let Some(ty) = recv_ty {
if let Some(field_ty) = ty.record_field_ty(db, *method_name) {
return CompleteDiagnostic {
severity: Severity::Error,
message,
sub_diagnostics: vec![SubDiagnostic {
style: LabelStyle::Primary,
message: format!(
"field `{}` in `{}` has type `{}`",
method_str,
recv_name,
field_ty.pretty_print(db)
),
span: primary.resolve(db),
}],
notes: vec![],
error_code,
};
}
}

CompleteDiagnostic {
severity: Severity::Error,
message,
sub_diagnostics: vec![SubDiagnostic {
style: LabelStyle::Primary,
message: format!("method not found in `{}`", recv_name),
span: primary.resolve(db),
}],
notes: vec![],
error_code,
}
}

Self::NotValue { primary, given } => CompleteDiagnostic {
severity: Severity::Error,
message: "value is expected".to_string(),
Expand Down
15 changes: 14 additions & 1 deletion crates/hir-analysis/src/name_resolution/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use either::Either;
use hir::{
hir_def::{IdentId, TopLevelMod},
span::DynLazySpan,
Expand All @@ -6,7 +7,10 @@ use salsa::Update;
use thin_vec::ThinVec;

use super::NameRes;
use crate::HirAnalysisDb;
use crate::{
ty::{trait_def::TraitDef, ty_def::TyId},
HirAnalysisDb,
};

#[derive(Debug, Clone, PartialEq, Eq, Hash, Update)]
pub enum NameResDiag<'db> {
Expand All @@ -16,6 +20,12 @@ pub enum NameResDiag<'db> {
/// The name is not found.
NotFound(DynLazySpan<'db>, IdentId<'db>),

MethodNotFound {
primary: DynLazySpan<'db>,
method_name: IdentId<'db>,
receiver: Either<TyId<'db>, TraitDef<'db>>,
},

/// The resolved name is not visible.
Invisible(DynLazySpan<'db>, IdentId<'db>, Option<DynLazySpan<'db>>),

Expand All @@ -27,6 +37,7 @@ pub enum NameResDiag<'db> {

TooManyGenericArgs {
span: DynLazySpan<'db>,
ty: TyId<'db>,
expected: u16,
given: u16,
},
Expand Down Expand Up @@ -54,6 +65,7 @@ impl<'db> NameResDiag<'db> {
.min()
.unwrap(),
Self::NotFound(span, _) => span.top_mod(db).unwrap(),
Self::MethodNotFound { primary, .. } => primary.top_mod(db).unwrap(),
Self::Invisible(span, _, _) => span.top_mod(db).unwrap(),
Self::Ambiguous(span, _, _) => span.top_mod(db).unwrap(),
Self::InvalidPathSegment(span, _, _) => span.top_mod(db).unwrap(),
Expand Down Expand Up @@ -88,6 +100,7 @@ impl<'db> NameResDiag<'db> {
Self::ExpectedTrait(..) => 7,
Self::ExpectedValue(..) => 8,
Self::TooManyGenericArgs { .. } => 9,
Self::MethodNotFound { .. } => 10,
}
}
}
Loading
Loading