Skip to content

Commit

Permalink
Do not use make use of InferenceResult::has_errors flag for mir bui…
Browse files Browse the repository at this point in the history
…lding

It generaly does not work as expected right now as we fallback type parameters to errors
  • Loading branch information
Veykril committed Feb 3, 2025
1 parent bec452f commit 5acbff8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/tools/rust-analyzer/crates/hir-ty/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,9 @@ pub struct InferenceResult {
pub type_of_for_iterator: FxHashMap<ExprId, Ty>,
type_mismatches: FxHashMap<ExprOrPatId, TypeMismatch>,
/// Whether there are any type-mismatching errors in the result.
// FIXME: This isn't as useful as initially thought due to us falling back placeholders to
// `TyKind::Error`.
// Which will then mark this field.
pub(crate) has_errors: bool,
/// Interned common types to return references to.
// FIXME: Move this into `InferenceContext`
Expand Down
21 changes: 17 additions & 4 deletions src/tools/rust-analyzer/crates/hir-ty/src/method_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! and the corresponding code mostly in rustc_hir_analysis/check/method/probe.rs.
use std::ops::ControlFlow;

use arrayvec::ArrayVec;
use base_db::CrateId;
use chalk_ir::{cast::Cast, UniverseIndex, WithKind};
use hir_def::{
Expand Down Expand Up @@ -732,15 +733,27 @@ fn lookup_impl_assoc_item_for_trait_ref(
let self_ty = trait_ref.self_type_parameter(Interner);
let self_ty_fp = TyFingerprint::for_trait_impl(&self_ty)?;
let impls = db.trait_impls_in_deps(env.krate);
let self_impls = match self_ty.kind(Interner) {
TyKind::Adt(id, _) => {
id.0.module(db.upcast()).containing_block().and_then(|it| db.trait_impls_in_block(it))

let trait_module = hir_trait_id.module(db.upcast());
let type_module = match self_ty_fp {
TyFingerprint::Adt(adt_id) => Some(adt_id.module(db.upcast())),
TyFingerprint::ForeignType(type_id) => {
Some(from_foreign_def_id(type_id).module(db.upcast()))
}
TyFingerprint::Dyn(trait_id) => Some(trait_id.module(db.upcast())),
_ => None,
};

let def_blocks: ArrayVec<_, 2> =
[trait_module.containing_block(), type_module.and_then(|it| it.containing_block())]
.into_iter()
.flatten()
.filter_map(|block_id| db.trait_impls_in_block(block_id))
.collect();

let impls = impls
.iter()
.chain(self_impls.as_ref())
.chain(&def_blocks)
.flat_map(|impls| impls.for_trait_and_self_ty(hir_trait_id, self_ty_fp));

let table = InferenceTable::new(db, env);
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/crates/hir-ty/src/mir/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2156,7 +2156,7 @@ pub fn lower_to_mir(
// need to take this input explicitly.
root_expr: ExprId,
) -> Result<MirBody> {
if infer.has_errors {
if infer.type_mismatches().next().is_some() {
return Err(MirLowerError::HasErrors);
}
let mut ctx = MirLowerCtx::new(db, owner, body, infer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,7 @@ fn func() {
fn override_lint_level() {
check_diagnostics(
r#"
#![allow(unused_variables)]
#[warn(nonstandard_style)]
fn foo() {
let BAR;
Expand Down Expand Up @@ -992,6 +993,7 @@ struct QUX;
const foo: i32 = 0;
fn BAR() {
let BAZ;
_ = BAZ;
}
"#,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -831,13 +831,14 @@ fn f() {

#[test]
fn or_pattern() {
// FIXME: `None` is inferred as unknown here for some reason
check_diagnostics(
r#"
//- minicore: option
fn f(_: i32) {}
fn main() {
let ((Some(mut x), None) | (_, Some(mut x))) = (None, Some(7)) else { return };
//^^^^^ 💡 warn: variable does not need to be mutable
f(x);
}
"#,
Expand Down

0 comments on commit 5acbff8

Please sign in to comment.