Repository: edgedb/edgedb

Issue:
`AssertionError: Calling new() on a context other than the current one` on empty cast with access policy
<!-- Please search existing issues to avoid creating duplicates. -->

<!--
For the EdgeDB Version: run `edgedb query 'select sys::get_version_as_str()'` from your project directory (or run `select sys::get_version_as_str();` in the EdgeDB interactive shell).
For the EdgeDB CLI Version: Run `edgedb --version` from anywhere
-->

- EdgeDB Version: 3.3
- EdgeDB CLI Version: 3.5.0
- OS Version: macos 13.5

Steps to Reproduce:

1. Select empty set as an object that has an access policy,
```
edgedb> select <X>{};
edgedb error: InternalServerError: AssertionError: Calling new() on a context other than the current one
  Hint: This is most likely a bug in EdgeDB. Please consider opening an issue ticket at https://github.com/edgedb/edgedb/issues/new?template=bug_report.md
  Server traceback:
      Traceback (most recent call last):
        File "/Users/jack/Library/Application Support/edgedb/portable/3.3/lib/python3.11/site-packages/edb/server/compiler_pool/worker.py", line 161, in compile
          units, cstate = COMPILER.compile(
                          ^^^^^^^^^^^^^^^^^
        File "/Users/jack/Library/Application Support/edgedb/portable/3.3/lib/python3.11/site-packages/edb/server/compiler/compiler.py", line 856, in compile
          unit_group = compile(ctx=ctx, source=source)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/Users/jack/Library/Application Support/edgedb/portable/3.3/lib/python3.11/site-packages/edb/server/compiler/compiler.py", line 2045, in compile
          return _try_compile(ctx=ctx, source=source)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/Users/jack/Library/Application Support/edgedb/portable/3.3/lib/python3.11/site-packages/edb/server/compiler/compiler.py", line 2113, in _try_compile
          comp, capabilities = _compile_dispatch_ql(
                               ^^^^^^^^^^^^^^^^^^^^^
...
```

<!-- If the issue is about a query error, please also provide your schema -->

Schema:
```
type X {
    access policy example
     deny all
     using (true);
}
```


Discussion:

Resolving patch:
diff --git a/edb/edgeql/compiler/viewgen.py b/edb/edgeql/compiler/viewgen.py
--- a/edb/edgeql/compiler/viewgen.py
+++ b/edb/edgeql/compiler/viewgen.py
@@ -2106,7 +2106,7 @@ def _inline_type_computable(
             # we see through that.
             base_stype = stype.get_nearest_non_derived_parent(ctx.env.schema)
             base_ir_set = setgen.ensure_set(
-                ir_set, type_override=base_stype, ctx=ctx)
+                ir_set, type_override=base_stype, ctx=scopectx)
 
             scopectx.anchors[qlast.Source().name] = base_ir_set
             ptr, ptr_set = _normalize_view_ptr_expr(


Test patch:
diff --git a/tests/test_edgeql_policies.py b/tests/test_edgeql_policies.py
--- a/tests/test_edgeql_policies.py
+++ b/tests/test_edgeql_policies.py
@@ -1287,3 +1287,12 @@ async def test_edgeql_policies_parent_update_01(self):
             await self.con.query('''
                 update Base set { name := '!!!' }
             ''')
+
+    async def test_edgeql_policies_empty_cast_01(self):
+        obj = await self.con._fetchall(
+            '''
+                SELECT <Issue>{}
+            ''',
+            __typenames__=True,
+        )
+        self.assertEqual(obj, [])