Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/jfecher/ante
Browse files Browse the repository at this point in the history
  • Loading branch information
jfecher committed Jan 12, 2025
2 parents 1873068 + 7541205 commit a6dfb25
Show file tree
Hide file tree
Showing 40 changed files with 556 additions and 456 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ The project itself provides build instructions for the [Nix package manager](htt
Those can be used for the most recent version of the compiler, or for working on it.

To enter the development environment, run either `nix-shell` or `nix develop` depending on whether you are using nix
with [flakes](https://nixos.wiki/wiki/Flakes) and [nix command](https://nixos.wiki/wiki/Nix_command) enabled or not.
with [flakes](https://wiki.nixos.org/wiki/Flakes) and [nix command](https://wiki.nixos.org/wiki/Nix_command) enabled or not.
Then you can build and run the project with `cargo` as described at the top of this section.

Beyond that, the project will also build with `nix-build` / `nix build`, meaning you can install it on your system using
Expand Down
2 changes: 1 addition & 1 deletion ante-ls/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl LanguageServer for Backend {
let name = v.kind.name();

let value =
typeprinter::show_type_and_traits(&name, typ, &info.required_traits, &info.trait_info, &cache);
typeprinter::show_type_and_traits(&name, typ, &info.required_traits, &info.trait_info, &cache, false);

let location = v.locate();
let range =
Expand Down
2 changes: 1 addition & 1 deletion examples/codegen/mutability.an
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ print num
mutate num
print num

mutate (n: &mut I32) =
mutate (n: !I32) =
x = double @n
n := x

Expand Down
2 changes: 1 addition & 1 deletion examples/codegen/pass_by_ref.an
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ foo (x: S) (y: S) =
printne "x before: "
print x
z = mut y
z.&message := "modifying y"
z.!message := "modifying y"
printne "x after: "
print x

Expand Down
2 changes: 1 addition & 1 deletion examples/codegen/string_builder.an
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import StringBuilder

sb: &mut StringBuilder = mut empty ()
sb: !StringBuilder = mut empty ()
reserve sb 10
append sb "你好,"
append sb " World!"
Expand Down
6 changes: 3 additions & 3 deletions examples/regressions/129_int_defaulting_generalization.an
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ second b + 2i64

// args: --check --show-types
// expected stdout:
// a : (U64, I64)
// b : (U64, I64)
// c : (U64, I64)
// a : U64, I64
// b : U64, I64
// c : U64, I64

// Expected results with the function block uncommented:
// f : (forall a. (Unit -> (U64, I64) can a))
Expand Down
8 changes: 4 additions & 4 deletions examples/typechecking/bind.an
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ add_one x =

// args: --check --show-types
// expected stdout:
// add_one : (forall a. ((Maybe I32) -> (Maybe I32) can a))
// bind : (forall a b c d. ((Maybe c) - (c => (Maybe b) can d) -> (Maybe b) can d))
// ret : (forall a b. (a -> (Maybe a) can b))
// x : (Maybe I32)
// add_one : forall a. (Maybe I32 -> Maybe I32 can a)
// bind : forall a b c d. (Maybe c - (c => Maybe b can d) -> Maybe b can d)
// ret : forall a b. (a -> Maybe a can b)
// x : Maybe I32
2 changes: 1 addition & 1 deletion examples/typechecking/completeness_checking.an
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ match (1, 2, 3, 4)
// completeness_checking.an:20:1 error: Missing case (false, false)
// match (true, true)
//
// completeness_checking.an:25:4 error: This pattern of type ((Int a), (Int b)) does not match the type ((Int a), ((Int b), ((Int c), (Int d)))) that is being matched on
// completeness_checking.an:25:4 error: This pattern of type Int a, Int b does not match the type Int a, Int b, Int c, Int d that is being matched on
// | (1, 2) -> 1
12 changes: 6 additions & 6 deletions examples/typechecking/effects.an
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ does_use x =

// args: --check --show-types
// expected stdout:
// does_use : (forall a b. (a -> Unit can (Use a, b)))
// does_use : forall a b. (a -> Unit can (Use a, b))
// given Add a
// get : (forall a b. (Unit -> a can (Use a, b)))
// handle_basic : (forall a. (Unit -> Unit can (Use String, a)))
// log : (forall a. (String -> Unit can (Log, a)))
// set : (forall a b. (a -> Unit can (Use a, b)))
// use_resume : (forall a. (Unit -> Unit can a))
// get : forall a b. (Unit -> a can (Use a, b))
// handle_basic : forall a. (Unit -> Unit can (Use String, a))
// log : forall a. (String -> Unit can (Log, a))
// set : forall a b. (a -> Unit can (Use a, b))
// use_resume : forall a. (Unit -> Unit can a)
8 changes: 4 additions & 4 deletions examples/typechecking/extern.an
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ exit2 0

// args: --check --show-types
// expected stdout:
// add : (forall a. (I32 - I32 -> I32 can a))
// exit2 : (forall a b. (I32 -> a can b))
// foo : (forall a b c. (a -> b can c))
// puts2 : (forall a. (String -> Unit can a))
// add : forall a. (I32 - I32 -> I32 can a)
// exit2 : forall a b. (I32 -> a can b)
// foo : forall a b c. (a -> b can c)
// puts2 : forall a. (String -> Unit can a)
6 changes: 3 additions & 3 deletions examples/typechecking/functor_and_monad.an
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ impl Monad Maybe with

// args: --check --show-types
// expected stdout:
// bind : (forall a b c d e. ((a b) - (b -> (a c) can d) -> (a c) can e))
// bind : forall a b c d e. (a b - (b -> a c can d) -> a c can e)
// given Monad a
// map : (forall a b c d e. ((a b) - (b -> c can d) -> (a c) can e))
// map : forall a b c d e. (a b - (b -> c can d) -> a c can e)
// given Functor a
// wrap : (forall a b c. (b -> (a b) can c))
// wrap : forall a b c. (b -> a b can c)
// given Monad a
2 changes: 1 addition & 1 deletion examples/typechecking/generalization.an
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ foo x =

// args: --check --show-types
// expected stdout:
// foo : (forall a b c d. (a -> (b => a can c) can d))
// foo : forall a b c d. (a -> b => a can c can d)
2 changes: 1 addition & 1 deletion examples/typechecking/impl.an
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ c = foo "one" "two"
// a : I32
// b : F64
// c : String
// foo : (forall a b. (a - a -> a can b))
// foo : forall a b. (a - a -> a can b)
// given Foo a
10 changes: 5 additions & 5 deletions examples/typechecking/instantiation.an
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ id x = x

// args: --check --show-types
// expected stdout:
// add : (forall a b c d e f g h i. ((a - c => e can g) - (a - b => c can g) -> (a => (b => e can g) can h) can i))
// id : (forall a b. (a -> a can b))
// one : (forall a b c d. ((a => b can d) - a -> b can d))
// two1 : (forall a b c. ((a => a can c) - a -> a can c))
// two2 : ((a => a can c) => (a => a can c) can d)
// add : forall a b c d e f g h i. ((a - c => e can g) - (a - b => c can g) -> a => b => e can g can h can i)
// id : forall a b. (a -> a can b)
// one : forall a b c d. ((a => b can d) - a -> b can d)
// two1 : forall a b c. ((b => b can c) - b -> b can c)
// two2 : (a => a can c) => a => a can c can d
10 changes: 5 additions & 5 deletions examples/typechecking/member_access.an
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ foo_and_bar foo bar
//

// expected stdout:
// Bar : (forall a. (Char -> Bar can a))
// Foo : (forall a. (F64 -> Foo can a))
// FooBar : (forall a. (I32 - String -> FooBar can a))
// Bar : forall a. (Char -> Bar can a)
// Foo : forall a. (F64 -> Foo can a)
// FooBar : forall a. (I32 - String -> FooBar can a)
// bar : Bar
// foo : Foo
// foo_and_bar : (forall a b c d. ({ foo: a, ..b } - { bar: String, ..c } -> String can d))
// foo_and_bar : forall a b c d. ({ foo: a, ..b } - { bar: String, ..c } -> String can d)
// foobar : FooBar
// stringify : (forall a. (String -> String can a))
// stringify : forall a. (String -> String can a)
8 changes: 4 additions & 4 deletions examples/typechecking/mutual_recursion.an
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ is_even 4
// TODO: is_odd here uses `forall a c.` instead of `forall a b.`

// expected stdout:
// is_even : (forall a b. ((Int a) -> Bool can b))
// given Eq (Int a), Print (Int a), Sub (Int a)
// is_odd : (forall a c. ((Int a) -> Bool can c))
// given Eq (Int a), Print (Int a), Sub (Int a)
// is_even : forall a b. (Int a -> Bool can b)
// given Eq Int a, Print Int a, Sub Int a
// is_odd : forall a c. (Int a -> Bool can c)
// given Eq Int a, Print Int a, Sub Int a
6 changes: 3 additions & 3 deletions examples/typechecking/named_constructor.an
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ foo = hello_foo 42

// args: --check --show-types
// expected stdout:
// Foo : (forall a b c. (a - b -> (Foo a b) can c))
// foo : (Foo String (Int a))
// hello_foo : (forall a b. (a -> (Foo String a) can b))
// Foo : forall a b c. (a - b -> Foo a b can c)
// foo : Foo String (Int a)
// hello_foo : forall a b. (a -> Foo String a can b)
2 changes: 1 addition & 1 deletion examples/typechecking/repeated_traits.an
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ foo a =
// Make sure output is not "... given Print a, Print a"
// args: --check --show-types
// expected stdout:
// foo : (forall a b. (a -> Unit can b))
// foo : forall a b. (a -> Unit can b)
// given Print a
2 changes: 1 addition & 1 deletion examples/typechecking/trait_fundep_result.an
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ str = foo 0i32

// args: --check --show-types
// expected stdout:
// foo : (forall a b c. (a -> b can c))
// foo : forall a b c. (a -> b can c)
// given Foo a b
// str : String
2 changes: 1 addition & 1 deletion examples/typechecking/trait_generalization.an
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
a = "test".c_string

// args: --check --show-types
// expected stdout: a : (Ptr Char)
// expected stdout: a : Ptr Char
6 changes: 3 additions & 3 deletions examples/typechecking/trait_propagation.an
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ foo () = baz bar
//
// args: --check --show-types
// expected stdout:
// bar : (forall a. a)
// baz : (forall a b. (a -> Unit can b))
// bar : forall a. a
// baz : forall a b. (a -> Unit can b)
// given Baz a
// foo : (forall a. (Unit -> Unit can a))
// foo : forall a. (Unit -> Unit can a)

// expected stderr:
// trait_propagation.an:6:10 error: No impl found for Baz a
Expand Down
10 changes: 5 additions & 5 deletions examples/typechecking/type_annotations.an
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ puts2
//

// expected stdout:
// bar : (forall a. (I32 - I32 -> I32 can a))
// baz : (forall a b. (Usz -> (Ptr a) can b))
// exit2 : (forall a b. (I32 -> a can b))
// foo : (forall a. (I32 - String -> Char can a))
// puts2 : (forall a. ((Ptr Char) -> I32 can a))
// bar : forall a. (I32 - I32 -> I32 can a)
// baz : forall a b. (Usz -> Ptr a can b)
// exit2 : forall a b. (I32 -> a can b)
// foo : forall a. (I32 - String -> Char can a)
// puts2 : forall a. (Ptr Char -> I32 can a)
57 changes: 31 additions & 26 deletions src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub enum DiagnosticKind {
NoMatchingImpls(/*constraint*/ String),
UnreachablePattern,
MissingCase(/*case*/ String),
UnhandledEffectsInMain(/*effects*/ String),
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
Expand Down Expand Up @@ -152,7 +153,7 @@ impl Display for DiagnosticKind {
DiagnosticKind::PreviouslyDefinedHere(item) => {
write!(f, "{} was previously defined here", item)
},
DiagnosticKind::IncorrectConstructorArgCount(item, expected, actual) => {
DiagnosticKind::IncorrectConstructorArgCount(item, actual, expected) => {
let plural_s = if *expected == 1 { "" } else { "s" };
let is_are = if *actual == 1 { "is" } else { "are" };
write!(
Expand All @@ -161,7 +162,7 @@ impl Display for DiagnosticKind {
item, expected, plural_s, actual, is_are
)
},
DiagnosticKind::IncorrectImplTraitArgCount(trait_name, expected, actual) => {
DiagnosticKind::IncorrectImplTraitArgCount(trait_name, actual, expected) => {
let plural_s = if *expected == 1 { "" } else { "s" };
write!(f, "impl has {} type argument{} but {} requires {}", expected, plural_s, trait_name, actual)
},
Expand Down Expand Up @@ -240,80 +241,80 @@ impl Display for DiagnosticKind {
"Invalid syntax in irrefutable pattern, expected a name, type annotation, or type constructor"
)
},
DiagnosticKind::FunctionParameterCountMismatch(typ, expected, actual) => {
DiagnosticKind::FunctionParameterCountMismatch(typ, actual, expected) => {
let plural_s = if *expected == 1 { "" } else { "s" };
let was_were = if *actual == 1 { "was" } else { "were" };
write!(f, "Function of type {typ} declared to take {expected} parameter{plural_s}, but {actual} {was_were} supplied")
},
DiagnosticKind::TypeError(TypeErrorKind::ExpectedUnitTypeFromPattern, _expected, actual) => {
DiagnosticKind::TypeError(TypeErrorKind::ExpectedUnitTypeFromPattern, actual, _expected) => {
write!(f, "Expected a unit type from this pattern, but the corresponding value has the type {}", actual)
},
DiagnosticKind::TypeError(TypeErrorKind::ExpectedPairTypeFromPattern, _, actual) => {
DiagnosticKind::TypeError(TypeErrorKind::ExpectedPairTypeFromPattern, actual, _expected) => {
write!(f, "Expected a pair type from this pattern, but found {actual}")
},
DiagnosticKind::TypeError(TypeErrorKind::VariableDoesNotMatchDeclaredType, expected, actual) => {
DiagnosticKind::TypeError(TypeErrorKind::VariableDoesNotMatchDeclaredType, actual, expected) => {
write!(f, "Variable type {actual} does not match its declared type of {expected}")
},
DiagnosticKind::TypeError(TypeErrorKind::PatternTypeDoesNotMatchAnnotatedType, expected, actual) => {
DiagnosticKind::TypeError(TypeErrorKind::PatternTypeDoesNotMatchAnnotatedType, actual, expected) => {
write!(f, "Pattern type {actual} does not match the annotated type {expected}")
},
DiagnosticKind::TypeError(TypeErrorKind::PatternTypeDoesNotMatchDefinitionType, expected, actual) => {
DiagnosticKind::TypeError(TypeErrorKind::PatternTypeDoesNotMatchDefinitionType, actual, expected) => {
write!(f, "Pattern type {actual} does not match the definition's type {expected}")
},
DiagnosticKind::TypeError(TypeErrorKind::FunctionBodyDoesNotMatchReturnType, expected, actual) => {
DiagnosticKind::TypeError(TypeErrorKind::FunctionBodyDoesNotMatchReturnType, actual, expected) => {
write!(f, "Function body type {actual} does not match declared return type of {expected}")
},
DiagnosticKind::TypeError(TypeErrorKind::CalledValueIsNotAFunction, _, actual) => {
DiagnosticKind::TypeError(TypeErrorKind::CalledValueIsNotAFunction, actual, _expected) => {
write!(f, "Value being called is not a function, it is a {actual}")
},
DiagnosticKind::TypeError(TypeErrorKind::ArgumentTypeMismatch, expected, actual) => {
DiagnosticKind::TypeError(TypeErrorKind::ArgumentTypeMismatch, actual, expected) => {
write!(f, "Expected argument of type {expected}, but found {actual}")
},
DiagnosticKind::TypeError(TypeErrorKind::NonBoolInCondition, expected, actual) => {
DiagnosticKind::TypeError(TypeErrorKind::NonBoolInCondition, actual, expected) => {
write!(f, "{actual} should be a {expected} to be used in an if condition")
},
DiagnosticKind::TypeError(TypeErrorKind::IfBranchMismatch, expected, actual) => {
DiagnosticKind::TypeError(TypeErrorKind::IfBranchMismatch, actual, expected) => {
write!(
f,
"Expected 'then' and 'else' branch types to match, but found {expected} and {actual} respectively"
)
},
DiagnosticKind::TypeError(TypeErrorKind::MatchPatternTypeDiffers, expected, actual) => {
DiagnosticKind::TypeError(TypeErrorKind::MatchPatternTypeDiffers, actual, expected) => {
write!(f, "This pattern of type {actual} does not match the type {expected} that is being matched on")
},
DiagnosticKind::TypeError(TypeErrorKind::MatchReturnTypeDiffers, expected, actual) => {
DiagnosticKind::TypeError(TypeErrorKind::MatchReturnTypeDiffers, actual, expected) => {
write!(
f,
"This branch's return type {actual} does not match the previous branches which return {expected}"
)
},
DiagnosticKind::TypeError(TypeErrorKind::DoesNotMatchAnnotatedType, expected, actual) => {
DiagnosticKind::TypeError(TypeErrorKind::DoesNotMatchAnnotatedType, actual, expected) => {
write!(f, "Expression of type {actual} does not match its annotated type {expected}")
},
DiagnosticKind::TypeError(TypeErrorKind::ExpectedStructReference, _, actual) => {
DiagnosticKind::TypeError(TypeErrorKind::ExpectedStructReference, actual, _expected) => {
write!(f, "Expected a struct reference but found {actual} instead")
},
DiagnosticKind::TypeError(TypeErrorKind::NoFieldOfType(field_name), expected, actual) => {
DiagnosticKind::TypeError(TypeErrorKind::NoFieldOfType(field_name), actual, expected) => {
write!(f, "{actual} has no field '{field_name}' of type {expected}")
},
DiagnosticKind::TypeError(TypeErrorKind::AssignToNonMutRef, expected, actual) => {
write!(f, "Expression of type {actual} must be a `{expected}` type to be assigned to")
DiagnosticKind::TypeError(TypeErrorKind::AssignToNonMutRef, actual, expected) => {
write!(f, "Expression of type {actual} must be a mutable reference type ({expected}) to be assigned to")
},
DiagnosticKind::TypeError(TypeErrorKind::AssignToWrongType, expected, actual) => {
DiagnosticKind::TypeError(TypeErrorKind::AssignToWrongType, actual, expected) => {
write!(f, "Cannot assign expression of type {actual} to a Ref of type {expected}")
},
DiagnosticKind::TypeError(TypeErrorKind::HandleBranchMismatch, expected, actual) => {
DiagnosticKind::TypeError(TypeErrorKind::HandleBranchMismatch, actual, expected) => {
write!(f, "The type of this branch ({actual}) should match the type of the expression being handled: {expected}")
},
DiagnosticKind::TypeError(TypeErrorKind::PatternReturnTypeMismatch, expected, actual) => {
DiagnosticKind::TypeError(TypeErrorKind::PatternReturnTypeMismatch, actual, expected) => {
write!(f, "Expected type {expected} does not match the pattern's return type {actual}")
},
DiagnosticKind::TypeError(TypeErrorKind::NeverShown, expected, actual) => {
DiagnosticKind::TypeError(TypeErrorKind::NeverShown, actual, expected) => {
unreachable!("This type error should never be shown. Expected {}, Actual {}", expected, actual)
},
DiagnosticKind::TypeError(TypeErrorKind::MonomorphizationError, expected, actual) => {
DiagnosticKind::TypeError(TypeErrorKind::MonomorphizationError, actual, expected) => {
unreachable!(
"Unification error during monomorphisation: Could not unify definition {} with instantiation {}",
"Unification error during monomorphization: Could not unify definition {} with instantiation {}",
expected, actual
)
},
Expand All @@ -335,6 +336,9 @@ impl Display for DiagnosticKind {
DiagnosticKind::MissingCase(case) => {
write!(f, "Missing case {case}")
},
DiagnosticKind::UnhandledEffectsInMain(effects) => {
write!(f, "Unhandled effects at top-level: {effects}")
},
}
}
}
Expand Down Expand Up @@ -381,6 +385,7 @@ impl DiagnosticKind {
| InternalError(_)
| NotAStruct(_)
| MissingFields(_)
| UnhandledEffectsInMain(_)
| NotAStructField(_) => Error,
}
}
Expand Down
Loading

0 comments on commit a6dfb25

Please sign in to comment.