Skip to content

Commit

Permalink
Report only first inference error for each location and kind
Browse files Browse the repository at this point in the history
  • Loading branch information
yuvalsw committed Mar 19, 2024
1 parent 0932932 commit 4c407fa
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 657 deletions.
2 changes: 1 addition & 1 deletion crates/cairo-lang-semantic/src/expr/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ pub fn compute_root_expr(

// Check fully resolved.
if let Some(inference_errs) = ctx.resolver.inference().finalize() {
inference_errs.report(ctx.diagnostics, syntax.stable_ptr().untyped());
inference_errs.report_firsts(ctx.diagnostics, syntax.stable_ptr().untyped());
return Ok(res);
}

Expand Down
32 changes: 30 additions & 2 deletions crates/cairo-lang-semantic/src/expr/inference.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Bidirectional type inference.

use std::collections::{HashMap, VecDeque};
use std::collections::{HashMap, HashSet, VecDeque};
use std::hash::Hash;
use std::ops::{Deref, DerefMut};

Expand Down Expand Up @@ -379,7 +379,7 @@ impl InferenceErrors {
pub fn push(&mut self, stable_ptr: Option<SyntaxStablePtrId>, err: InferenceError) {
self.errors.push((stable_ptr, err));
}
pub fn report(
pub fn report_all(
&self,
diagnostics: &mut SemanticDiagnostics,
default_stable_ptr: SyntaxStablePtrId,
Expand All @@ -388,6 +388,34 @@ impl InferenceErrors {
err.report(diagnostics, stable_ptr.unwrap_or(default_stable_ptr));
}
}
pub fn report_firsts(
&self,
diagnostics: &mut SemanticDiagnostics,
default_stable_ptr: SyntaxStablePtrId,
) {
let mut reported = HashSet::new();
for (stable_ptr, err) in &self.errors {
let location = stable_ptr.unwrap_or(default_stable_ptr);
// TODO(yg): is there a better way?
let err_kind = match err {
InferenceError::Failed(_) => 1,
InferenceError::Cycle { .. } => 2,
InferenceError::TypeKindMismatch { .. } => 3,
InferenceError::ConstKindMismatch { .. } => 4,
InferenceError::ImplKindMismatch { .. } => 5,
InferenceError::GenericArgMismatch { .. } => 6,
InferenceError::TraitMismatch { .. } => 7,
InferenceError::GenericFunctionMismatch { .. } => 8,
InferenceError::ConstInferenceNotSupported => 9,
InferenceError::NoImplsFound { .. } => 10,
InferenceError::Ambiguity(_) => 11,
InferenceError::TypeNotInferred { .. } => 12,
};
if reported.insert((location, err_kind)) {
err.report(diagnostics, location);
}
}
}
pub fn is_empty(&self) -> bool {
self.errors.is_empty()
}
Expand Down
100 changes: 0 additions & 100 deletions crates/cairo-lang-semantic/src/expr/test_data/coupon
Original file line number Diff line number Diff line change
Expand Up @@ -131,36 +131,6 @@ error: Type annotations needed. Failed to infer ?8
let mut y = get_coupon();
^********^

error: Type annotations needed. Failed to infer ?9
--> lib.cairo:22:17
let mut y = get_coupon();
^********^

error: Type annotations needed. Failed to infer ?10
--> lib.cairo:22:17
let mut y = get_coupon();
^********^

error: Type annotations needed. Failed to infer ?11
--> lib.cairo:22:17
let mut y = get_coupon();
^********^

error: Type annotations needed. Failed to infer ?12
--> lib.cairo:22:17
let mut y = get_coupon();
^********^

error: Type annotations needed. Failed to infer ?13
--> lib.cairo:22:17
let mut y = get_coupon();
^********^

error: Type annotations needed. Failed to infer ?14
--> lib.cairo:22:17
let mut y = get_coupon();
^********^

//! > ==========================================================================

//! > Test finding impls.
Expand Down Expand Up @@ -253,76 +223,6 @@ error: Type annotations needed. Failed to infer ?0
bar(1); // Cannot infer S.
^*^

error: Type annotations needed. Failed to infer ?1
--> lib.cairo:7:5
bar(1); // Cannot infer S.
^*^

error: Type annotations needed. Failed to infer ?2
--> lib.cairo:7:5
bar(1); // Cannot infer S.
^*^

error: Type annotations needed. Failed to infer ?3
--> lib.cairo:7:5
bar(1); // Cannot infer S.
^*^

error: Type annotations needed. Failed to infer ?4
--> lib.cairo:7:5
bar(1); // Cannot infer S.
^*^

error: Type annotations needed. Failed to infer ?5
--> lib.cairo:7:5
bar(1); // Cannot infer S.
^*^

error: Type annotations needed. Failed to infer ?6
--> lib.cairo:7:5
bar(1); // Cannot infer S.
^*^

error: Type annotations needed. Failed to infer ?7
--> lib.cairo:7:5
bar(1); // Cannot infer S.
^*^

error: Type annotations needed. Failed to infer ?8
--> lib.cairo:7:5
bar(1); // Cannot infer S.
^*^

error: Type annotations needed. Failed to infer ?9
--> lib.cairo:7:5
bar(1); // Cannot infer S.
^*^

error: Type annotations needed. Failed to infer ?10
--> lib.cairo:7:5
bar(1); // Cannot infer S.
^*^

error: Type annotations needed. Failed to infer ?11
--> lib.cairo:7:5
bar(1); // Cannot infer S.
^*^

error: Type annotations needed. Failed to infer ?12
--> lib.cairo:7:5
bar(1); // Cannot infer S.
^*^

error: Type annotations needed. Failed to infer ?13
--> lib.cairo:7:5
bar(1); // Cannot infer S.
^*^

error: Type annotations needed. Failed to infer ?14
--> lib.cairo:7:5
bar(1); // Cannot infer S.
^*^

//! > ==========================================================================

//! > Test coupon disabled.
Expand Down
Loading

0 comments on commit 4c407fa

Please sign in to comment.