Point guard constant type errors at the guard
This commit is contained in:
1 parent
305f0f4ec3
commit
4e06b77490
5 files changed
+130
-1
No files matched your search
@@ -638,6 +638,25 @@ impl<A> Constant<A> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
pub fn with_location(mut self, new_location: SrcSpan) -> Self {
|
||||||
|
match &mut self {
|
||||||
|
Constant::Int { location, .. }
|
||||||
|
| Constant::List { location, .. }
|
||||||
|
| Constant::Float { location, .. }
|
||||||
|
| Constant::Tuple { location, .. }
|
||||||
|
| Constant::String { location, .. }
|
||||||
|
| Constant::Record { location, .. }
|
||||||
|
| Constant::RecordUpdate { location, .. }
|
||||||
|
| Constant::BitArray { location, .. }
|
||||||
|
| Constant::Var { location, .. }
|
||||||
|
| Constant::Invalid { location, .. }
|
||||||
|
| Constant::Todo { location, .. }
|
||||||
|
| Constant::BinaryOperator { location, .. } => *location = new_location,
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn can_have_multiple_per_line(&self) -> bool {
|
pub fn can_have_multiple_per_line(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
|
|||||||
@@ -320,7 +320,8 @@ impl<'expression_typer, 'env, 'module> GuardTyper<'expression_typer, 'env, 'modu
|
|||||||
}
|
}
|
||||||
|
|
||||||
ValueConstructorVariant::ModuleConstant { literal, .. } => {
|
ValueConstructorVariant::ModuleConstant { literal, .. } => {
|
||||||
return Ok(ClauseGuard::Constant(literal.clone()));
|
let literal = literal.clone().with_location(location);
|
||||||
|
return Ok(ClauseGuard::Constant(literal));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -87,3 +87,42 @@ pub fn main(wibble: wibble.Wibble) {
|
|||||||
vec![("main", "fn(Wibble) -> Bool")]
|
vec![("main", "fn(Wibble) -> Bool")]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn constant_with_wrong_type_in_guard() {
|
||||||
|
assert_module_error!(
|
||||||
|
r#"
|
||||||
|
pub type Wibble {
|
||||||
|
Wibble(String)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const constant = Wibble("Hi")
|
||||||
|
|
||||||
|
pub fn main() {
|
||||||
|
let wibble = "wibble"
|
||||||
|
case 1 {
|
||||||
|
_ if wibble == constant -> Nil
|
||||||
|
_ -> Nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn unqualified_imported_constant_with_wrong_type_in_guard() {
|
||||||
|
assert_module_error!(
|
||||||
|
("wibble", "pub const constant = 1"),
|
||||||
|
r#"
|
||||||
|
import wibble.{constant}
|
||||||
|
|
||||||
|
pub fn main() {
|
||||||
|
let wibble = "wibble"
|
||||||
|
case 1 {
|
||||||
|
_ if wibble == constant -> Nil
|
||||||
|
_ -> Nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#
|
||||||
|
);
|
||||||
|
}
|
||||||
+35
@@ -0,0 +1,35 @@
|
|||||||
|
---
|
||||||
|
source: compiler-core/src/type_/tests/guards.rs
|
||||||
|
expression: "\npub type Wibble {\n Wibble(String)\n}\n\npub const constant = Wibble(\"Hi\")\n\npub fn main() {\n let wibble = \"wibble\"\n case 1 {\n _ if wibble == constant -> Nil\n _ -> Nil\n }\n}\n"
|
||||||
|
---
|
||||||
|
----- SOURCE CODE
|
||||||
|
|
||||||
|
pub type Wibble {
|
||||||
|
Wibble(String)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const constant = Wibble("Hi")
|
||||||
|
|
||||||
|
pub fn main() {
|
||||||
|
let wibble = "wibble"
|
||||||
|
case 1 {
|
||||||
|
_ if wibble == constant -> Nil
|
||||||
|
_ -> Nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
----- ERROR
|
||||||
|
error: Type mismatch
|
||||||
|
┌─ /src/one/two.gleam:11:20
|
||||||
|
│
|
||||||
|
11 │ _ if wibble == constant -> Nil
|
||||||
|
│ ^^^^^^^^
|
||||||
|
|
||||||
|
Expected type:
|
||||||
|
|
||||||
|
String
|
||||||
|
|
||||||
|
Found type:
|
||||||
|
|
||||||
|
Wibble
|
||||||
+35
@@ -0,0 +1,35 @@
|
|||||||
|
---
|
||||||
|
source: compiler-core/src/type_/tests/guards.rs
|
||||||
|
expression: "\nimport wibble.{constant}\n\npub fn main() {\n let wibble = \"wibble\"\n case 1 {\n _ if wibble == constant -> Nil\n _ -> Nil\n }\n}\n"
|
||||||
|
---
|
||||||
|
----- SOURCE CODE
|
||||||
|
-- wibble.gleam
|
||||||
|
pub const constant = 1
|
||||||
|
|
||||||
|
-- main.gleam
|
||||||
|
|
||||||
|
import wibble.{constant}
|
||||||
|
|
||||||
|
pub fn main() {
|
||||||
|
let wibble = "wibble"
|
||||||
|
case 1 {
|
||||||
|
_ if wibble == constant -> Nil
|
||||||
|
_ -> Nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
----- ERROR
|
||||||
|
error: Type mismatch
|
||||||
|
┌─ /src/one/two.gleam:7:20
|
||||||
|
│
|
||||||
|
7 │ _ if wibble == constant -> Nil
|
||||||
|
│ ^^^^^^^^
|
||||||
|
|
||||||
|
Expected type:
|
||||||
|
|
||||||
|
String
|
||||||
|
|
||||||
|
Found type:
|
||||||
|
|
||||||
|
Int
|
||||||
Reference in new issue
Block a user