Skip to content

Commit 426d284

Browse files
committed
Auto merge of rust-lang#16222 - rosefromthedead:unresolved-assoc-item, r=Veykril
add unresolved-assoc-item assist I tried to copy from private-assoc-item for this
2 parents 86e559b + 5878651 commit 426d284

File tree

6 files changed

+74
-1
lines changed

6 files changed

+74
-1
lines changed

crates/hir-ty/src/infer.rs

+3
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ pub enum InferenceDiagnostic {
219219
field_with_same_name: Option<Ty>,
220220
assoc_func_with_same_name: Option<AssocItemId>,
221221
},
222+
UnresolvedAssocItem {
223+
id: ExprOrPatId,
224+
},
222225
// FIXME: This should be emitted in body lowering
223226
BreakOutsideOfLoop {
224227
expr: ExprId,

crates/hir-ty/src/infer/path.rs

+3
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,9 @@ impl InferenceContext<'_> {
340340
},
341341
);
342342
let res = res.or(not_visible);
343+
if res.is_none() {
344+
self.push_diagnostic(InferenceDiagnostic::UnresolvedAssocItem { id });
345+
}
343346
let (item, visible) = res?;
344347

345348
let (def, container) = match item {

crates/hir/src/diagnostics.rs

+6
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ diagnostics![
6262
UndeclaredLabel,
6363
UnimplementedBuiltinMacro,
6464
UnreachableLabel,
65+
UnresolvedAssocItem,
6566
UnresolvedExternCrate,
6667
UnresolvedField,
6768
UnresolvedImport,
@@ -218,6 +219,11 @@ pub struct UnresolvedMethodCall {
218219
pub assoc_func_with_same_name: Option<AssocItemId>,
219220
}
220221

222+
#[derive(Debug)]
223+
pub struct UnresolvedAssocItem {
224+
pub expr_or_pat: InFile<AstPtr<Either<ast::Expr, Either<ast::Pat, ast::SelfParam>>>>,
225+
}
226+
221227
#[derive(Debug)]
222228
pub struct PrivateField {
223229
pub expr: InFile<AstPtr<ast::Expr>>,

crates/hir/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1697,6 +1697,13 @@ impl DefWithBody {
16971697
.into(),
16981698
)
16991699
}
1700+
&hir_ty::InferenceDiagnostic::UnresolvedAssocItem { id } => {
1701+
let expr_or_pat = match id {
1702+
ExprOrPatId::ExprId(expr) => expr_syntax(expr).map(AstPtr::wrap_left),
1703+
ExprOrPatId::PatId(pat) => pat_syntax(pat).map(AstPtr::wrap_right),
1704+
};
1705+
acc.push(UnresolvedAssocItem { expr_or_pat }.into())
1706+
}
17001707
&hir_ty::InferenceDiagnostic::BreakOutsideOfLoop {
17011708
expr,
17021709
is_break,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
2+
3+
// Diagnostic: unresolved-assoc-item
4+
//
5+
// This diagnostic is triggered if the referenced associated item does not exist.
6+
pub(crate) fn unresolved_assoc_item(
7+
ctx: &DiagnosticsContext<'_>,
8+
d: &hir::UnresolvedAssocItem,
9+
) -> Diagnostic {
10+
Diagnostic::new_with_syntax_node_ptr(
11+
ctx,
12+
DiagnosticCode::RustcHardError("E0599"),
13+
"no such associated item",
14+
d.expr_or_pat.clone().map(Into::into),
15+
)
16+
}
17+
18+
#[cfg(test)]
19+
mod tests {
20+
use crate::tests::check_diagnostics;
21+
22+
#[test]
23+
fn bare() {
24+
check_diagnostics(
25+
r#"
26+
struct S;
27+
28+
fn main() {
29+
let _ = S::Assoc;
30+
//^^^^^^^^ error: no such associated item
31+
}
32+
"#,
33+
);
34+
}
35+
36+
#[test]
37+
fn unimplemented_trait() {
38+
check_diagnostics(
39+
r#"
40+
struct S;
41+
trait Foo {
42+
const X: u32;
43+
}
44+
45+
fn main() {
46+
let _ = S::X;
47+
//^^^^ error: no such associated item
48+
}
49+
"#,
50+
);
51+
}
52+
}

crates/ide-diagnostics/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ mod handlers {
5151
pub(crate) mod typed_hole;
5252
pub(crate) mod type_mismatch;
5353
pub(crate) mod unimplemented_builtin_macro;
54+
pub(crate) mod unresolved_assoc_item;
5455
pub(crate) mod unresolved_extern_crate;
5556
pub(crate) mod unresolved_field;
5657
pub(crate) mod unresolved_method;
@@ -371,7 +372,8 @@ pub fn diagnostics(
371372
AnyDiagnostic::TypeMismatch(d) => handlers::type_mismatch::type_mismatch(&ctx, &d),
372373
AnyDiagnostic::UndeclaredLabel(d) => handlers::undeclared_label::undeclared_label(&ctx, &d),
373374
AnyDiagnostic::UnimplementedBuiltinMacro(d) => handlers::unimplemented_builtin_macro::unimplemented_builtin_macro(&ctx, &d),
374-
AnyDiagnostic::UnreachableLabel(d) => handlers::unreachable_label:: unreachable_label(&ctx, &d),
375+
AnyDiagnostic::UnreachableLabel(d) => handlers::unreachable_label::unreachable_label(&ctx, &d),
376+
AnyDiagnostic::UnresolvedAssocItem(d) => handlers::unresolved_assoc_item::unresolved_assoc_item(&ctx, &d),
375377
AnyDiagnostic::UnresolvedExternCrate(d) => handlers::unresolved_extern_crate::unresolved_extern_crate(&ctx, &d),
376378
AnyDiagnostic::UnresolvedField(d) => handlers::unresolved_field::unresolved_field(&ctx, &d),
377379
AnyDiagnostic::UnresolvedImport(d) => handlers::unresolved_import::unresolved_import(&ctx, &d),

0 commit comments

Comments
 (0)