Skip to content
Merged
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
8 changes: 8 additions & 0 deletions compiler/noirc_frontend/src/elaborator/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ impl Elaborator<'_> {
}
};

if fields.len() != field_types.len() {
self.push_err(TypeCheckError::TupleMismatch {
tuple_types: field_types.clone(),
actual_count: fields.len(),
location,
});
}

let fields = vecmap(fields.into_iter().enumerate(), |(i, field)| {
let field_type = field_types.get(i).cloned().unwrap_or(Type::Error);
self.elaborate_pattern_mut(
Expand Down
15 changes: 14 additions & 1 deletion compiler/noirc_frontend/src/hir/type_check/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::BTreeSet;
use std::rc::Rc;

use acvm::FieldElement;
use iter_extended::vecmap;
use noirc_errors::CustomDiagnostic as Diagnostic;
use noirc_errors::Location;
use thiserror::Error;
Expand Down Expand Up @@ -249,6 +250,8 @@ pub enum TypeCheckError {
/// This error is used for types like integers which have too many variants to enumerate
#[error("Missing cases: `{typ}` is non-empty")]
MissingManyCases { typ: String, location: Location },
#[error("Expected a tuple with {} elements, found one with {} elements", tuple_types.len(), actual_count)]
TupleMismatch { tuple_types: Vec<Type>, actual_count: usize, location: Location },
}

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -340,7 +343,8 @@ impl TypeCheckError {
| TypeCheckError::UnreachableCase { location }
| TypeCheckError::MissingCases { location, .. }
| TypeCheckError::MissingManyCases { location, .. }
| TypeCheckError::NestedUnsafeBlock { location } => *location,
| TypeCheckError::NestedUnsafeBlock { location }
| TypeCheckError::TupleMismatch { location, .. } => *location,

TypeCheckError::DuplicateNamedTypeArg { name: ident, .. }
| TypeCheckError::NoSuchNamedTypeArg { name: ident, .. } => ident.location(),
Expand Down Expand Up @@ -726,6 +730,15 @@ impl<'a> From<&'a TypeCheckError> for Diagnostic {
let secondary = "Try adding a match-all pattern: `_`".to_string();
Diagnostic::simple_error(msg, secondary, *location)
},
TypeCheckError::TupleMismatch { tuple_types, actual_count, location } => {
let msg = format!(
"Expected a tuple with {} elements, found one with {} elements",
tuple_types.len(),
actual_count
);
let secondary = format!("The expression the tuple is assigned to has type `({})`", vecmap(tuple_types, ToString::to_string).join(","));
Diagnostic::simple_error(msg, secondary, *location)
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions test_programs/compile_failure/tuple_mismatch/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "tuple_mismatch"
type = "bin"
authors = [""]
compiler_version = ">=0.33.0"

[dependencies]
3 changes: 3 additions & 0 deletions test_programs/compile_failure/tuple_mismatch/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
let (_x, _y) = (1, 2, 3);
}
8 changes: 8 additions & 0 deletions test_programs/compile_failure/tuple_mismatch/stderr.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: Expected a tuple with 3 elements, found one with 2 elements
┌─ src/main.nr:2:9
2 │ let (_x, _y) = (1, 2, 3);
│ -------- The expression the tuple is assigned to has type `(Field,Field,Field)`

Aborting due to 1 previous error
Loading