Skip to content

Commit

Permalink
Calling: covariance and contravariance. Casts & more
Browse files Browse the repository at this point in the history
  • Loading branch information
kaleidawave committed Feb 1, 2024
1 parent c27208b commit dfd38b3
Show file tree
Hide file tree
Showing 53 changed files with 1,465 additions and 1,082 deletions.
Binary file modified checker/definitions/internal.ts.d.bin
Binary file not shown.
18 changes: 18 additions & 0 deletions checker/examples/cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! This generates a cache from a definition file
use std::{
env,
fs::{read_to_string, write},
};

use ezno_checker::{generate_cache, synthesis::EznoParser};

fn main() {
let input = env::args().nth(1).expect("expected path to definition file");
let output = env::args().nth(2).expect("expected output path");

let reader = |path: &std::path::Path| read_to_string(path).ok();
let cache = generate_cache::<_, EznoParser>(input.into(), reader, ());
write(output, cache).unwrap();
eprintln!("Cache generated 🏧💵✅")
}
2 changes: 1 addition & 1 deletion checker/examples/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn main() {
if args.iter().any(|arg| arg == "--events") {
eprintln!("Events on entry:");
let (_, entry_module) = result.modules.into_iter().next().unwrap();
for item in entry_module.facts.get_events() {
for item in entry_module.info.get_events() {
eprintln!("\t{item:?}");
}
}
Expand Down
2 changes: 1 addition & 1 deletion checker/specification/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ fn markdown_lines_append_test_to_rust(

fn heading_to_rust_identifier(heading: &str) -> String {
heading
.replace([' ', '-', '&'], "_")
.replace([' ', '-', '/', '&'], "_")
.replace(['*', '\'', '`', '"', '!', '(', ')', ','], "")
.to_lowercase()
}
42 changes: 28 additions & 14 deletions checker/specification/specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ let b: boolean = a
const a = c
```

- Could not find variable c in scope
- Could not find variable 'c' in scope

#### Assignment to non-existent variable

```ts
doesNotExist = 4;
```

- Cannot assign to unknown variable 'doesNotExist'

#### Variable declared twice

Expand All @@ -63,7 +71,7 @@ a satisfies 2;
const a = 3;
```

- Cannot redeclare variable a
- Cannot redeclare variable 'a'

#### Unintialised variables are undefined

Expand Down Expand Up @@ -341,8 +349,8 @@ func satisfies (a: string, b: number) => string;
func satisfies (a: number, b: number) => boolean;
```

- Expected (a: string, b: number) => string, found (a: string, b: number) => true
- Expected (a: number, b: number) => boolean, found (a: string, b: number) => true
- Expected (a: string, b: number) => string, found (a: string, b: number) => boolean
- Expected (a: number, b: number) => boolean, found (a: string, b: number) => boolean

#### Function that throws returns never

Expand Down Expand Up @@ -429,9 +437,9 @@ function alterParameter(a: number, b: { prop: string }) {

b.prop = 3;

// Observed
b.prop = "hello";
b.prop satisfies "hello";
// Observed. TODO disabled because of possible impure (getters etc)
// b.prop satisfies "hello";
}
```

Expand Down Expand Up @@ -494,7 +502,7 @@ function func<T>(a: T) {}
func<number>("hello world")
```

- Argument of type "hello world" is not assignable to parameter of type number
- Argument of type "hello world" is not assignable to parameter of type T

#### Get value of property on parameter

Expand Down Expand Up @@ -780,7 +788,7 @@ getX();
let x: number = 5;
```

- Variable x used before declaration
- Variable 'x' used before declaration

> Not shown in the example but thanks to [#69](https://github.com/kaleidawave/ezno/pull/69) for adding the position of the error
Expand All @@ -791,18 +799,24 @@ let x: number = 5;
```ts
let myObject: { a: number } = { a: 4 }

function readA(someObject: { a: number | string }) {
return someObject.a;
}

function setAtoString(someObject: { a: number | string }) {
someObject.a = "hi";
}

// Allowed
readA(myObject);
setAtoString({ a: 6 });
setAtoString(myObject);
```

> Error message could be better. Full one contains labels with more information
- Assignment mismatch

> Error message could be better. Full diagnostic contains labels with more information
> `readA` is allowed, which is disallowed in Hegel, but here is allowed to preserve TSC compatibility (and because how structural subtyping is implemented)
> Not shown in the example but thanks to [#69](https://github.com/kaleidawave/ezno/pull/69) for adding the position of the error
#### Property assignment from conditional
Expand Down Expand Up @@ -1496,12 +1510,12 @@ type X = { a: string }
#### TDZ in statements

```ts
let x = y;
let first = second;

let y = 2;
let second = 2;
```

- Variable y used before declaration
- Variable 'second' used before declaration

### Classes

Expand Down Expand Up @@ -2003,7 +2017,7 @@ export const x = 2;
const y = "122LH"
```

- Could not find variable y in scope
- Could not find variable 'y' in scope

#### Import side effect

Expand Down
83 changes: 82 additions & 1 deletion checker/specification/staging.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,44 @@ const obj: MyObject = {

- Expected number, found string

### Collection
#### Return type annotation is used in constraint

> While could use the returned type (as done in the second example). Using the annotation prevents other code breaking if the body changes
> As shown later, this doesn't affect what is returned if called
```ts
function getNumber1(): number {
return 4
}

function getNumber2() {
return 6
}

getNumber1 satisfies () => 4;
getNumber2 satisfies () => 6;

getNumber1() satisfies 4;
getNumber2() satisfies 6;
```

- Expected () => 4, found () => number

#### Generic argument/constraint leads to inference

```ts
function callFunction<T>(fn: (p: T) => void) {
// ...
}

callFunction<string>(a => {
a satisfies number;
})
```

- Expected number, found string

### Collections

> TODO other arguments
Expand Down Expand Up @@ -157,3 +194,47 @@ declare let aNumber: number;
```

- Expected 4, found 2

### Types

#### And object constraint

```ts
const obj = { a: 2, b: 3 };
obj.c = obj;
const second: { a: number, c: { b: number } } = obj;

obj.a = "hi";
obj.b = "hello";
```

- Type "hi" does not meet property constraint number
- Type "hello" does not meet property constraint number

#### Double generics

> Really want to only have one covariant and one contravariant but want to keep TSC semantics
```ts
declare function what<T>(a: T, b: T): T;

what(2, 3) satisfies string;
```

- Expected string, found 2 | 3

#### As casts

> Disabled normally, allowed for these tests. Provides TSC compatibility and because narrowing not implemented (including secret feature)
```ts
declare let global: any;

5 as boolean;
global satisfies boolean;
(global as string) satisfies number;
```

- Cannot cast 5 to boolean
- Expected boolean, found any
- Expected number, found string
20 changes: 20 additions & 0 deletions checker/specification/to_implement.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,26 @@ function add() {

- Type "hi" is not assignable to argument of type number

#### Interface extends

```ts
interface X {
a: string
}

interface Y {
b: string
}

interface Z extends X, Y {
c: string
}

({ c: "hi" }) satisfies Z;
```

- Type { c: "hi" } is not assignable to Z

### Narrowing

> TODO `typeof`, `instanceof`, conditional, across a function
Expand Down
4 changes: 0 additions & 4 deletions checker/src/context/bases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ pub struct Bases {
}

impl Bases {
pub(crate) fn _does_type_have_mutable_base(&self, _on: TypeId) -> bool {
todo!()
}

pub(crate) fn merge(&mut self, bases: Bases, context_id: ContextId) {
self.immutable_bases.extend(bases.immutable_bases);
for (ty, (ctx_ceil, base)) in bases.mutable_bases {
Expand Down
Loading

0 comments on commit dfd38b3

Please sign in to comment.