Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add static typechecker #26

Merged
merged 145 commits into from
Feb 24, 2024
Merged
Changes from 1 commit
Commits
Show all changes
145 commits
Select commit Hold shift + click to select a range
7d31cb1
Add ideas about multithreading
ruuda Jan 1, 2024
0e46d15
Add initial static typechecker
ruuda Jan 1, 2024
2695b96
Enable the typechecker on the evaluation path
ruuda Jan 1, 2024
2cf49a0
Report unknown variables from the typechecker
ruuda Jan 1, 2024
9c3081d
Implement typechecks for statements
ruuda Jan 1, 2024
af18f08
Remove now-redundant runtime typechecks
ruuda Jan 2, 2024
0855969
Define a type lattice after all
ruuda Jan 4, 2024
1439238
Implement simple type expr evaluator
ruuda Jan 4, 2024
9f82e4b
Implement typecheck for sequences
ruuda Jan 5, 2024
d92be8e
Add spans to BinOp
ruuda Jan 5, 2024
ebcf60f
Slightly simplify span handling in parser
ruuda Jan 5, 2024
5864254
Typecheck function calls partially
ruuda Jan 5, 2024
e500164
Handle field lookups in the typechecker
ruuda Jan 5, 2024
0fb1cd6
Implement typecheck for for loops
ruuda Jan 5, 2024
61c024a
Implement typechecking for indexing
ruuda Jan 5, 2024
d1e61d5
Make some type errors a bit briefer
ruuda Jan 5, 2024
c6642e8
Typecheck union, make error reporting more flexible
ruuda Jan 16, 2024
d05e9bc
Work on typechecking functions
ruuda Jan 22, 2024
ea40756
Store function types in the AST nodes
ruuda Jan 22, 2024
7823333
Typecheck function compatibility at runtime
ruuda Jan 22, 2024
e32be14
Typecheck calls to statically known functions
ruuda Jan 22, 2024
f0d34f7
Add prelude for typecheck phase
ruuda Jan 22, 2024
fbd47ea
Add regression test for missing var in fstring
ruuda Jan 22, 2024
3114724
Update expected output for static arity error
ruuda Jan 22, 2024
900a60e
Update goldens for type errors
ruuda Jan 22, 2024
80fc243
Update golden for static not callable error
ruuda Jan 22, 2024
49827ab
Add golden for stack overflow bug
ruuda Jan 24, 2024
11d3003
Add eval depth check on collection literals
ruuda Jan 24, 2024
a4a4f1f
Add reminder to add type error reason in condition
ruuda Jan 28, 2024
636ea97
Update golden output for new type error messages
ruuda Jan 28, 2024
791e4aa
Update expected output for seq goldens
ruuda Jan 28, 2024
d66e77b
Update goldens for call arity errors
ruuda Jan 28, 2024
33e9563
Update final few goldens for new type errors
ruuda Jan 28, 2024
f775e31
Accept Clippy advice
ruuda Jan 28, 2024
9a6e969
Expose unimplemented calls to the fuzzer again
ruuda Jan 28, 2024
28957b6
Update goldens for new type errors
ruuda Jan 29, 2024
7c53724
Delete pre-static type evaluator
ruuda Jan 30, 2024
6e2a39a
Add a Rust eDSL to annotate stdlib with types
ruuda Feb 2, 2024
8065778
Typecheck values of built-in functions and methods
ruuda Feb 2, 2024
866fa72
Allow writing naming the Dynamic type
ruuda Feb 3, 2024
ad8296c
Document that record types are planned
ruuda Feb 4, 2024
a1af998
Add regression test for + operator
ruuda Feb 5, 2024
f29c3d8
Unify type error reporting, add sanity check
ruuda Feb 5, 2024
3f2e71d
Fix issue with typechecking lists
ruuda Feb 5, 2024
9c006dc
Add another regression test for typechecker
ruuda Feb 5, 2024
806b4a9
Handle Void type in the typechecker
ruuda Feb 5, 2024
42a8378
Prevent another case of Dynamic in type error
ruuda Feb 7, 2024
aa6e82a
"Handle" another case of Dynamic in error
ruuda Feb 7, 2024
1770dd8
Write down some thoughts on how to evolve typecheck
ruuda Feb 7, 2024
8031f9a
Write down some more typecheck ideas
ruuda Feb 7, 2024
e1612f9
Define type requirements
ruuda Feb 10, 2024
34b807c
Define TypeDiff, work on TypeReq
ruuda Feb 10, 2024
861fd2c
Refine the type requirements
ruuda Feb 10, 2024
1eb3848
Move type env into the Typechecker struct
ruuda Feb 10, 2024
e1da872
Bring new TypeReq partiall into the typechecker
ruuda Feb 10, 2024
fee7bce
Add context to new-style type errors
ruuda Feb 10, 2024
397bcf6
Port some AST nodes to the new type check
ruuda Feb 11, 2024
0a41b05
Port a few more cases to the new typechecker
ruuda Feb 11, 2024
5871d84
Port some of function types to the new check
ruuda Feb 11, 2024
c5a0857
Port calls and indexing to new typecheck
ruuda Feb 11, 2024
c923188
Implement most operators in new-style typecheck
ruuda Feb 11, 2024
8dec298
Port all typechecks to the new TypeReq machinery
ruuda Feb 11, 2024
3670b2b
Delete old machinery for value typechecks
ruuda Feb 11, 2024
62414d0
Correct two typechecking bugs
ruuda Feb 11, 2024
49e2362
Construct function type from type requirement
ruuda Feb 11, 2024
acf9b9d
Record entire span for type expressions
ruuda Feb 11, 2024
3b0b70f
Extend type requirement in collection literals
ruuda Feb 11, 2024
77f5c28
Add test for annotated empty set literals
ruuda Feb 11, 2024
9f1eadb
Include spans necessary for error reporting
ruuda Feb 11, 2024
c31b9a4
Correct goldens for desirable new error messages
ruuda Feb 11, 2024
d953f03
Typecheck function types
ruuda Feb 11, 2024
0545854
Delete now-unused subtype check code
ruuda Feb 11, 2024
2950469
Make type mismatch printing more reusable
ruuda Feb 11, 2024
b5944a5
Typecheck more about function literals
ruuda Feb 11, 2024
40442d9
Define SourcedType, rewrite the typechecker again
ruuda Feb 11, 2024
bbb554d
Bring back Void type error, track sources
ruuda Feb 12, 2024
a0cd0aa
Accept new output in goldens
ruuda Feb 12, 2024
35ea923
Track source spans for literals
ruuda Feb 12, 2024
fa31336
Rename "Dynamic" to "Any"
ruuda Feb 12, 2024
4d3c8c6
Document that the typechecker may reject programs
ruuda Feb 12, 2024
8d96ae8
Add golden for example runtime error from the docs
ruuda Feb 12, 2024
c689237
Document the Void type
ruuda Feb 12, 2024
f17e32f
Include type error explanation in value error
ruuda Feb 12, 2024
3d4e061
Resolve to do in one of the goldens
ruuda Feb 12, 2024
5718396
Update goldens for new output
ruuda Feb 13, 2024
9dbee67
Record entire span of functions, not just =>
ruuda Feb 13, 2024
fda08d7
Add argument names to function types
ruuda Feb 13, 2024
c068f74
Unify arity checks and error reporting
ruuda Feb 13, 2024
2833233
Restore arity checks on builtin functions
ruuda Feb 13, 2024
833c475
Simplify and fix call frames in calls from builtins
ruuda Feb 13, 2024
23034b7
Update goldens for new error messages
ruuda Feb 13, 2024
b1471c9
Add idea for this call frame mess
ruuda Feb 13, 2024
bb34ecb
Adjust stack depth counter again to avoid overflow
ruuda Feb 15, 2024
39d03da
Delete unused function
ruuda Feb 15, 2024
8dc890c
Add some goldens to cover type functionalities
ruuda Feb 15, 2024
5bab50a
Implement subtyping for functions
ruuda Feb 15, 2024
a24f73e
Report runtime type error on function defer
ruuda Feb 17, 2024
45a3d9a
Fix the variance of function args check
ruuda Feb 17, 2024
abb3347
Enable static typechecks of function arguments
ruuda Feb 17, 2024
0e72502
Add goldens to test runtime dict type errors
ruuda Feb 17, 2024
07f17a9
Allow arbitrary values in error value path
ruuda Feb 17, 2024
c58e7ff
Add goldens for function type runtime checks
ruuda Feb 17, 2024
c723076
Delete some more dead code
ruuda Feb 17, 2024
623d64d
Add golden for runtime check for Any
ruuda Feb 17, 2024
99f661e
Add goldens to cover function subtype checks
ruuda Feb 17, 2024
5b2bf4f
Add goldens to cover type errors in collections
ruuda Feb 17, 2024
2bfbec2
Rename goldens to have one directory for typecheck
ruuda Feb 17, 2024
3738f57
Add golden to cover type error formatting
ruuda Feb 17, 2024
a616e46
Exclude uncoverable asserts from coverage report
ruuda Feb 17, 2024
fd4ed70
Think about whether all values should be comparable
ruuda Feb 17, 2024
e36ae8b
Implement pretty-printing of TypeDiff
ruuda Feb 17, 2024
46a8181
Tweak the formatting of type errors a bit
ruuda Feb 17, 2024
2f6b8cd
Preserve and report more type source locations
ruuda Feb 18, 2024
6b072c2
Include span of actual type in error when far away
ruuda Feb 18, 2024
411fd55
Delete unused variant Source::Many
ruuda Feb 18, 2024
43d8ed7
Remove one resolved to do
ruuda Feb 18, 2024
8837675
Highlight the args span for std.range
ruuda Feb 21, 2024
ad3d740
Simplify call frame handling in evaluation
ruuda Feb 21, 2024
0fe100f
Be more cautious when replacing call frames
ruuda Feb 22, 2024
36481cb
Reorder type machinery into more modules
ruuda Feb 22, 2024
457a0b6
Delete now-dead method
ruuda Feb 22, 2024
04559e7
Add goldens to cover runtime iteration arity errors
ruuda Feb 22, 2024
d0e3c12
Simplify evaluation of Seq with type info
ruuda Feb 22, 2024
0e9ae9b
Simplify eval of Seq now that we have types
ruuda Feb 22, 2024
7339074
Add golden and update reachable code after typechecks
ruuda Feb 22, 2024
6b33044
Reserve capacity for list evaluation
ruuda Feb 22, 2024
7da6330
Add goldens for indexing into a string
ruuda Feb 22, 2024
661a03e
Explain index errors better, add goldens
ruuda Feb 22, 2024
546e705
Add golden for non-string assertion message
ruuda Feb 22, 2024
a188254
Add golden for runtime non-iterable error
ruuda Feb 22, 2024
70bb7e6
Delete more dead code, add coverage markers
ruuda Feb 22, 2024
bc18daf
Add coverage for statically abusing union operator
ruuda Feb 22, 2024
3f08155
Add goldens for type expr parsing
ruuda Feb 23, 2024
817fb35
Add more goldens to cover the typechecker
ruuda Feb 23, 2024
15a6be0
Add goldens for type_source coverage
ruuda Feb 23, 2024
e8e4ef7
Remove premature optimization
ruuda Feb 23, 2024
ae8daa0
Correct names of golden tests after typechecker
ruuda Feb 23, 2024
c1afd80
Remove duplicated comment
ruuda Feb 24, 2024
09342d8
Simplify type source check
ruuda Feb 24, 2024
a813982
Refactor type error explanation messages
ruuda Feb 24, 2024
361eae4
Remove leftover unused argument
ruuda Feb 24, 2024
ae9b467
Fix type signatures of key_by methods
ruuda Feb 24, 2024
0503f34
Remove resolved question
ruuda Feb 24, 2024
6a3ec5c
Document how to produce an empty set
ruuda Feb 24, 2024
414b2ea
Remove resolved to dos, fix typo in comment
ruuda Feb 24, 2024
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
Prev Previous commit
Next Next commit
Delete more dead code, add coverage markers
ruuda committed Feb 24, 2024
commit 70bb7e6ce54b676dc75096bc0a2e0c6bcc6ed9e3
11 changes: 5 additions & 6 deletions src/source.rs
Original file line number Diff line number Diff line change
@@ -25,11 +25,13 @@ pub type Inputs<'a> = [Doc<'a>];
#[derive(Copy, Clone, Eq, Ord, PartialEq, PartialOrd)]
pub struct DocId(pub u32);

// coverage:off -- Only used in assertion failures, which should be uncovered.
impl fmt::Debug for DocId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
// coverage:on

/// Marks a location in a source file by byte offset.
///
@@ -67,7 +69,9 @@ impl Span {
// with a panic.
assert!(
end <= 0xffff_ffff_ffff,
"Document should not be larger than 262 TiB."
// coverage:off -- Error is not tested, I don't have such large disk.
"Document should not be larger than 262 TiB.",
// coverage:on
);
debug_assert!(end >= start);
Span {
@@ -110,11 +114,6 @@ impl Span {
Span::new(self.doc(), self.start() + n_trim, self.end())
}

/// Keep at most the first `n` bytes of the span.
pub fn take(&self, n: usize) -> Span {
Span::new(self.doc(), self.start(), self.end().min(self.start() + n))
}

/// Return a span that runs from self up to but not including `other`.
pub fn until(&self, other: Span) -> Span {
debug_assert_eq!(self.doc(), other.doc());
4 changes: 3 additions & 1 deletion src/typecheck.rs
Original file line number Diff line number Diff line change
@@ -503,9 +503,11 @@ impl<'a> TypeChecker<'a> {
.check(expr_span)?
}

Expr::CheckType { .. } | Expr::TypedFunction { .. } | Expr::SetLit { .. } | Expr::DictLit { .. } => panic!(
// coverage:off -- Arm should be unreachable.
Expr::CheckType { .. } | Expr::TypedFunction { .. } | Expr::SetLit { .. } | Expr::DictLit { .. } => unreachable!(
"Node {expr:?} is inserted by the typechecker, it should not be present before checking."
),
// coverage:on
};
match expr_type {
// If the type check passed, great, we now know the inferred type.