Skip to content

Commit

Permalink
feat: add unresolved-ident diagnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
rosefromthedead authored and Veykril committed Feb 19, 2024
1 parent d818b53 commit a492d9d
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 26 deletions.
3 changes: 3 additions & 0 deletions crates/hir-ty/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ pub enum InferenceDiagnostic {
UnresolvedAssocItem {
id: ExprOrPatId,
},
UnresolvedIdent {
expr: ExprId,
},
// FIXME: This should be emitted in body lowering
BreakOutsideOfLoop {
expr: ExprId,
Expand Down
14 changes: 12 additions & 2 deletions crates/hir-ty/src/infer/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use hir_def::{
ArithOp, Array, BinaryOp, ClosureKind, Expr, ExprId, LabelId, Literal, Statement, UnaryOp,
},
lang_item::{LangItem, LangItemTarget},
path::{GenericArg, GenericArgs},
path::{GenericArg, GenericArgs, Path},
BlockId, ConstParamId, FieldId, ItemContainerId, Lookup, TupleFieldId, TupleId,
};
use hir_expand::name::{name, Name};
Expand Down Expand Up @@ -439,7 +439,17 @@ impl InferenceContext<'_> {
}
Expr::Path(p) => {
let g = self.resolver.update_to_inner_scope(self.db.upcast(), self.owner, tgt_expr);
let ty = self.infer_path(p, tgt_expr.into()).unwrap_or_else(|| self.err_ty());
let ty = match self.infer_path(p, tgt_expr.into()) {
Some(ty) => ty,
None => {
if matches!(p, Path::Normal { mod_path, .. } if mod_path.is_ident()) {
self.push_diagnostic(InferenceDiagnostic::UnresolvedIdent {
expr: tgt_expr,
});
}
self.err_ty()
}
};
self.resolver.reset_to_guard(g);
ty
}
Expand Down
10 changes: 10 additions & 0 deletions crates/hir/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ diagnostics![
UnresolvedMacroCall,
UnresolvedMethodCall,
UnresolvedModule,
UnresolvedIdent,
UnresolvedProcMacro,
UnusedMut,
UnusedVariable,
Expand Down Expand Up @@ -242,6 +243,11 @@ pub struct UnresolvedAssocItem {
pub expr_or_pat: InFile<AstPtr<Either<ast::Expr, Either<ast::Pat, ast::SelfParam>>>>,
}

#[derive(Debug)]
pub struct UnresolvedIdent {
pub expr: InFile<AstPtr<ast::Expr>>,
}

#[derive(Debug)]
pub struct PrivateField {
pub expr: InFile<AstPtr<ast::Expr>>,
Expand Down Expand Up @@ -588,6 +594,10 @@ impl AnyDiagnostic {
};
UnresolvedAssocItem { expr_or_pat }.into()
}
&InferenceDiagnostic::UnresolvedIdent { expr } => {
let expr = expr_syntax(expr);
UnresolvedIdent { expr }.into()
}
&InferenceDiagnostic::BreakOutsideOfLoop { expr, is_break, bad_value_break } => {
let expr = expr_syntax(expr);
BreakOutsideOfLoop { expr, is_break, bad_value_break }.into()
Expand Down
1 change: 1 addition & 0 deletions crates/ide-diagnostics/src/handlers/inactive_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fn f() {
#[cfg(a)] let x = 0; // let statement
//^^^^^^^^^^^^^^^^^^^^ weak: code is inactive due to #[cfg] directives: a is disabled
fn abc() {}
abc(#[cfg(a)] 0);
//^^^^^^^^^^^ weak: code is inactive due to #[cfg] directives: a is disabled
let x = Struct {
Expand Down
3 changes: 2 additions & 1 deletion crates/ide-diagnostics/src/handlers/missing_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,8 @@ struct TestStruct { one: i32, two: i64 }
fn test_fn() {
let one = 1;
let s = TestStruct{ ..a };
let a = TestStruct{ one, two: 2 };
let _ = TestStruct{ ..a };
}
"#,
);
Expand Down
16 changes: 10 additions & 6 deletions crates/ide-diagnostics/src/handlers/missing_match_arms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ pub(crate) fn missing_match_arms(
#[cfg(test)]
mod tests {
use crate::{
tests::{check_diagnostics, check_diagnostics_with_config},
tests::{
check_diagnostics, check_diagnostics_with_config, check_diagnostics_with_disabled,
},
DiagnosticsConfig,
};

Expand Down Expand Up @@ -282,7 +284,7 @@ fn main() {
cov_mark::check_count!(validate_match_bailed_out, 4);
// Match statements with arms that don't match the
// expression pattern do not fire this diagnostic.
check_diagnostics(
check_diagnostics_with_disabled(
r#"
enum Either { A, B }
enum Either2 { C, D }
Expand All @@ -307,6 +309,7 @@ fn main() {
match Unresolved::Bar { Unresolved::Baz => () }
}
"#,
&["E0425"],
);
}

Expand Down Expand Up @@ -397,11 +400,11 @@ fn main() {
match loop {} {
Either::A => (),
}
match loop { break Foo::A } {
//^^^^^^^^^^^^^^^^^^^^^ error: missing match arm: `B` not covered
match loop { break Either::A } {
//^^^^^^^^^^^^^^^^^^^^^^^^ error: missing match arm: `B` not covered
Either::A => (),
}
match loop { break Foo::A } {
match loop { break Either::A } {
Either::A => (),
Either::B => (),
}
Expand Down Expand Up @@ -977,7 +980,7 @@ fn f(ty: Enum) {
#[test]
fn unexpected_ty_fndef() {
cov_mark::check!(validate_match_bailed_out);
check_diagnostics(
check_diagnostics_with_disabled(
r"
enum Exp {
Tuple(()),
Expand All @@ -987,6 +990,7 @@ fn f() {
Exp::Tuple => {}
}
}",
&["E0425"],
);
}

Expand Down
25 changes: 15 additions & 10 deletions crates/ide-diagnostics/src/handlers/remove_unnecessary_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &RemoveUnnecessaryElse) -> Option<Vec<

#[cfg(test)]
mod tests {
use crate::tests::{check_diagnostics, check_diagnostics_with_disabled, check_fix};
use crate::tests::{check_diagnostics_with_disabled, check_fix};

#[test]
fn remove_unnecessary_else_for_return() {
Expand All @@ -110,7 +110,7 @@ fn test() {
}
}
"#,
&["needless_return"],
&["needless_return", "E0425"],
);
check_fix(
r#"
Expand Down Expand Up @@ -148,7 +148,7 @@ fn test() {
}
}
"#,
&["needless_return"],
&["needless_return", "E0425"],
);
check_fix(
r#"
Expand Down Expand Up @@ -227,7 +227,7 @@ fn test() {
}
}
"#,
&["needless_return"],
&["needless_return", "E0425"],
);
check_fix(
r#"
Expand Down Expand Up @@ -293,7 +293,7 @@ fn test() {

#[test]
fn remove_unnecessary_else_for_break() {
check_diagnostics(
check_diagnostics_with_disabled(
r#"
fn test() {
loop {
Expand All @@ -306,6 +306,7 @@ fn test() {
}
}
"#,
&["E0425"],
);
check_fix(
r#"
Expand Down Expand Up @@ -334,7 +335,7 @@ fn test() {

#[test]
fn remove_unnecessary_else_for_continue() {
check_diagnostics(
check_diagnostics_with_disabled(
r#"
fn test() {
loop {
Expand All @@ -347,6 +348,7 @@ fn test() {
}
}
"#,
&["E0425"],
);
check_fix(
r#"
Expand Down Expand Up @@ -375,7 +377,7 @@ fn test() {

#[test]
fn remove_unnecessary_else_for_never() {
check_diagnostics(
check_diagnostics_with_disabled(
r#"
fn test() {
if foo {
Expand All @@ -390,6 +392,7 @@ fn never() -> ! {
loop {}
}
"#,
&["E0425"],
);
check_fix(
r#"
Expand Down Expand Up @@ -422,7 +425,7 @@ fn never() -> ! {

#[test]
fn no_diagnostic_if_no_else_branch() {
check_diagnostics(
check_diagnostics_with_disabled(
r#"
fn test() {
if foo {
Expand All @@ -432,12 +435,13 @@ fn test() {
do_something_else();
}
"#,
&["E0425"],
);
}

#[test]
fn no_diagnostic_if_no_divergence() {
check_diagnostics(
check_diagnostics_with_disabled(
r#"
fn test() {
if foo {
Expand All @@ -447,6 +451,7 @@ fn test() {
}
}
"#,
&["E0425"],
);
}

Expand All @@ -462,7 +467,7 @@ fn test() {
}
}
"#,
&["needless_return"],
&["needless_return", "E0425"],
);
}

Expand Down
8 changes: 5 additions & 3 deletions crates/ide-diagnostics/src/handlers/undeclared_label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ fn foo() {
fn while_let_loop_with_label_in_condition() {
check_diagnostics(
r#"
//- minicore: option
fn foo() {
let mut optional = Some(0);
'my_label: while let Some(a) = match optional {
'my_label: while let Some(_) = match optional {
None => break 'my_label,
Some(val) => Some(val),
} {
Expand All @@ -59,8 +61,8 @@ fn foo() {
r#"
//- minicore: iterator
fn foo() {
'xxx: for _ in unknown {
'yyy: for _ in unknown {
'xxx: for _ in [] {
'yyy: for _ in [] {
break 'xxx;
continue 'yyy;
break 'zzz;
Expand Down
7 changes: 5 additions & 2 deletions crates/ide-diagnostics/src/handlers/unresolved_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ fn method_fix(
#[cfg(test)]
mod tests {
use crate::{
tests::{check_diagnostics, check_diagnostics_with_config},
tests::{
check_diagnostics, check_diagnostics_with_config, check_diagnostics_with_disabled,
},
DiagnosticsConfig,
};

Expand Down Expand Up @@ -148,14 +150,15 @@ fn foo() {

#[test]
fn no_diagnostic_on_unknown() {
check_diagnostics(
check_diagnostics_with_disabled(
r#"
fn foo() {
x.foo;
(&x).foo;
(&((x,),),).foo;
}
"#,
&["E0425"],
);
}

Expand Down
46 changes: 46 additions & 0 deletions crates/ide-diagnostics/src/handlers/unresolved_ident.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};

// Diagnostic: unresolved-ident
//
// This diagnostic is triggered if an expr-position ident is invalid.
pub(crate) fn unresolved_ident(
ctx: &DiagnosticsContext<'_>,
d: &hir::UnresolvedIdent,
) -> Diagnostic {
Diagnostic::new_with_syntax_node_ptr(
ctx,
DiagnosticCode::RustcHardError("E0425"),
"no such value in this scope",
d.expr.map(Into::into),
)
.experimental()
}

#[cfg(test)]
mod tests {
use crate::tests::check_diagnostics;

#[test]
fn missing() {
check_diagnostics(
r#"
fn main() {
let _ = x;
//^ error: no such value in this scope
}
"#,
);
}

#[test]
fn present() {
check_diagnostics(
r#"
fn main() {
let x = 5;
let _ = x;
}
"#,
);
}
}
4 changes: 2 additions & 2 deletions crates/ide-diagnostics/src/handlers/unresolved_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ fn main() {
r#"
struct Foo { bar: i32 }
fn foo() {
Foo { bar: i32 }.bar();
// ^^^ error: no method `bar` on type `Foo`, but a field with a similar name exists
Foo { bar: 0 }.bar();
// ^^^ error: no method `bar` on type `Foo`, but a field with a similar name exists
}
"#,
);
Expand Down
Loading

0 comments on commit a492d9d

Please sign in to comment.