Skip to content

Commit

Permalink
revert(compiler): mutable lifted object may be modified inflight (#6263)
Browse files Browse the repository at this point in the history
Reverts #6258

broke some winglibs due to strange behavior trying to call `copyMut` on immutable maps during inflight. See below

<img width="363" alt="image" src="https://github.com/winglang/wing/assets/45375125/89f09ba9-de35-4a01-b039-9330a2c3a586">
  • Loading branch information
hasanaburayyan authored Apr 17, 2024
1 parent d867af8 commit 797829a
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 126 deletions.
5 changes: 3 additions & 2 deletions docs/docs/02-concepts/01-preflight-and-inflight.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ new cloud.Function(checkEndpoint);
```

However, mutation to preflight data is not allowed.
This mean means that variables from preflight cannot be reassigned to, and mutable collections like `MutArray` and `MutMap` cannot be modified (they're turned into their immutable counterparts, `Array` and `Map`, respectively when accessed inflight).
This mean means that variables from preflight cannot be reassigned to, and mutable collections like `MutArray` and `MutMap` cannot be modified.

```js playground
let var count = 3;
Expand All @@ -263,7 +263,8 @@ names.push("Jack"); // OK

inflight () => {
count = count + 1; // error: Variable cannot be reassigned from inflight
names.push("Jill"); // error: push doesn't exist in Array
names.push("Jill"); // error: variable "names" cannot be mutated in inflight - error message not
// implemented yet, see https://github.com/winglang/wing/issues/3069
};
```

Expand Down
30 changes: 0 additions & 30 deletions examples/tests/invalid/un_mut_lifted_objects.test.w

This file was deleted.

32 changes: 4 additions & 28 deletions libs/wingc/src/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2846,11 +2846,6 @@ impl<'a> TypeChecker<'a> {
}
}(exp, env);

// If we're inflight but the expression is a lifted (preflight) expression then make it immutable
if env.phase == Phase::Inflight && phase == Phase::Preflight {
t = self.make_immutable(t);
}

self.types.assign_type_to_expr(exp, t, phase);

self.curr_expr_info.pop();
Expand Down Expand Up @@ -4493,6 +4488,10 @@ impl<'a> TypeChecker<'a> {

fn type_check_assignment(&mut self, kind: &AssignmentKind, value: &Expr, variable: &Reference, env: &mut SymbolEnv) {
let (exp_type, _) = self.type_check_exp(value, env);

// TODO: we need to verify that if this variable is defined in a parent environment (i.e.
// being captured) it cannot be reassigned: https://github.com/winglang/wing/issues/3069

let (var, var_phase) = self.resolve_reference(&variable, env, false);
let var_type = match &var {
ResolveReferenceResult::Variable(var) => var.type_,
Expand Down Expand Up @@ -5692,29 +5691,6 @@ impl<'a> TypeChecker<'a> {
.map(|_| base_udt)
}

fn make_immutable(&mut self, type_: TypeRef) -> TypeRef {
match *type_ {
Type::MutArray(inner) => {
let inner = self.make_immutable(inner);
self.types.add_type(Type::Array(inner))
}
Type::MutJson => self.types.json(),
Type::MutMap(inner) => {
let inner = self.make_immutable(inner);
self.types.add_type(Type::Map(inner))
}
Type::MutSet(inner) => {
let inner = self.make_immutable(inner);
self.types.add_type(Type::Set(inner))
}
Type::Optional(inner) => {
let inner = self.make_immutable(inner);
self.types.add_type(Type::Optional(inner))
}
_ => type_,
}
}

fn resolve_reference(
&mut self,
reference: &Reference,
Expand Down
66 changes: 0 additions & 66 deletions tools/hangar/__snapshots__/invalid.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4255,72 +4255,6 @@ error: Expected type to be \\"num\\", but got \\"str\\" instead



Tests 1 failed (1)
Snapshots 1 skipped
Test Files 1 failed (1)
Duration <DURATION>"
`;

exports[`un_mut_lifted_objects.test.w 1`] = `
"error: Member \\"push\\" doesn't exist in \\"Array\\"
--> ../../../examples/tests/invalid/un_mut_lifted_objects.test.w:22:6
|
22 | ar.push(2); // Error: push doesn't exist in Array
| ^^^^ Member \\"push\\" doesn't exist in \\"Array\\"


error: Cannot update elements of an immutable Array
--> ../../../examples/tests/invalid/un_mut_lifted_objects.test.w:23:3
|
23 | ar[0] = 1; // Error: Cannot update elements of an immutable Array
| ^^^^^ Cannot update elements of an immutable Array
|
= hint: Consider using MutArray instead


error: Member \\"set\\" doesn't exist in \\"Json\\"
--> ../../../examples/tests/invalid/un_mut_lifted_objects.test.w:24:5
|
24 | j.set(\\"a\\", 3); // Error: set doesn't exist in Json
| ^^^ Member \\"set\\" doesn't exist in \\"Json\\"


error: Member \\"add\\" doesn't exist in \\"Set\\"
--> ../../../examples/tests/invalid/un_mut_lifted_objects.test.w:25:6
|
25 | st.add(4); // Error: add doesn't exist in Set
| ^^^ Member \\"add\\" doesn't exist in \\"Set\\"


error: Member \\"set\\" doesn't exist in \\"Map\\"
--> ../../../examples/tests/invalid/un_mut_lifted_objects.test.w:26:6
|
26 | mp.set(\\"a\\", 3); // Error: set doesn't exist in Map
| ^^^ Member \\"set\\" doesn't exist in \\"Map\\"


error: Member \\"push\\" doesn't exist in \\"Array\\"
--> ../../../examples/tests/invalid/un_mut_lifted_objects.test.w:27:11
|
27 | opt_ar?.push(2); // Error: push doesn't exist in Array
| ^^^^ Member \\"push\\" doesn't exist in \\"Array\\"


error: Member \\"push\\" doesn't exist in \\"Array\\"
--> ../../../examples/tests/invalid/un_mut_lifted_objects.test.w:28:16
|
28 | recursive_ar.push(MutArray<num>[2]); // Error: push doesn't exist in Array
| ^^^^ Member \\"push\\" doesn't exist in \\"Array\\"


error: Member \\"push\\" doesn't exist in \\"Array\\"
--> ../../../examples/tests/invalid/un_mut_lifted_objects.test.w:29:22
|
29 | recursive_ar.at(0).push(3); // Error: push doesn't exist in Array
| ^^^^ Member \\"push\\" doesn't exist in \\"Array\\"



Tests 1 failed (1)
Snapshots 1 skipped
Test Files 1 failed (1)
Expand Down

0 comments on commit 797829a

Please sign in to comment.