Repository: edgedb/edgedb

Issue:
Using __subject__ in a default in SDL causes a tracer ISE
<!-- Please search existing issues to avoid creating duplicates. -->

```
edgedb error: InternalServerError: <QualName default::User3@name@default>
  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 "/home/aljaz/EdgeDB/edgedb/edb/server/compiler_pool/worker.py", line 161, in compile
          units, cstate = COMPILER.compile(
                          ^^^^^^^^^^^^^^^^^
        File "/home/aljaz/EdgeDB/edgedb/edb/server/compiler/compiler.py", line 755, in compile
          unit_group = compile(ctx=ctx, source=source)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/aljaz/EdgeDB/edgedb/edb/server/compiler/compiler.py", line 1941, in compile
          return _try_compile(ctx=ctx, source=source)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/aljaz/EdgeDB/edgedb/edb/server/compiler/compiler.py", line 2009, in _try_compile
          comp, capabilities = _compile_dispatch_ql(
                               ^^^^^^^^^^^^^^^^^^^^^
        File "/home/aljaz/EdgeDB/edgedb/edb/server/compiler/compiler.py", line 1848, in _compile_dispatch_ql
          query = ddl.compile_dispatch_ql_migration(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/aljaz/EdgeDB/edgedb/edb/server/compiler/ddl.py", line 381, in compile_dispatch_ql_migration
          return _start_migration(ctx, ql, in_script)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/aljaz/EdgeDB/edgedb/edb/server/compiler/ddl.py", line 462, in _start_migration
          target_schema = s_ddl.apply_sdl(
                          ^^^^^^^^^^^^^^^^
        File "/home/aljaz/EdgeDB/edgedb/edb/schema/ddl.py", line 513, in apply_sdl
          ddl_stmts = s_decl.sdl_to_ddl(target_schema, documents)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/aljaz/EdgeDB/edgedb/edb/edgeql/declarative.py", line 453, in sdl_to_ddl
          trace_dependencies(decl_ast, ctx=tracectx)
        File "/usr/lib/python3.11/functools.py", line 909, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/aljaz/EdgeDB/edgedb/edb/edgeql/declarative.py", line 1055, in trace_default
          _register_item(node, ctx=ctx)
        File "/home/aljaz/EdgeDB/edgedb/edb/edgeql/declarative.py", line 1189, in _register_item
          trace_dependencies(cmd, ctx=ctx)
        File "/usr/lib/python3.11/functools.py", line 909, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/aljaz/EdgeDB/edgedb/edb/edgeql/declarative.py", line 967, in trace_ConcretePointer
          _register_item(
        File "/home/aljaz/EdgeDB/edgedb/edb/edgeql/declarative.py", line 1189, in _register_item
          trace_dependencies(cmd, ctx=ctx)
        File "/usr/lib/python3.11/functools.py", line 909, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/aljaz/EdgeDB/edgedb/edb/edgeql/declarative.py", line 822, in trace_SetField
          _register_item(node, deps=deps, hard_dep_exprs=exprs, ctx=ctx)
        File "/home/aljaz/EdgeDB/edgedb/edb/edgeql/declarative.py", line 1210, in _register_item
          params = expr.params

        File "/home/aljaz/EdgeDB/edgedb/edb/edgeql/tracer.py", line 344, in trace_refs
          trace(qltree, ctx=ctx)
        File "/usr/lib/python3.11/functools.py", line 909, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/aljaz/EdgeDB/edgedb/edb/edgeql/tracer.py", line 1037, in trace_Select
          tip = trace(node.result, ctx=ctx)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/usr/lib/python3.11/functools.py", line 909, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/aljaz/EdgeDB/edgedb/edb/edgeql/tracer.py", line 875, in trace_Path
          tr = trace(step, ctx=ctx)
               ^^^^^^^^^^^^^^^^^^^^
        File "/usr/lib/python3.11/functools.py", line 909, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/aljaz/EdgeDB/edgedb/edb/edgeql/tracer.py", line 890, in trace_SpecialAnchor
          return ctx.objects[name]
                 ~~~~~~~~~~~^^^^^^
      KeyError: <QualName default::User3@name@default>
```


- EdgeDB Version: dev
- EdgeDB CLI Version: dev
- OS Version: Linux

Steps to Reproduce:

1. `edb cli migration create`

Schema:

```
module default {

    type User3 {

        required property nick: str;

        required property name: str {

            default := (select __subject__.nick);

        };
    }
    
}
```


Discussion:

Resolving patch:
diff --git a/edb/edgeql/declarative.py b/edb/edgeql/declarative.py
--- a/edb/edgeql/declarative.py
+++ b/edb/edgeql/declarative.py
@@ -1194,7 +1194,12 @@ def _register_item(
         anchors = dict(anchors or {})
         if source:
             anchors['__source__'] = source
-        if subject or fq_name:
+        if subject or (
+            fq_name
+            and not (
+                isinstance(decl, qlast.SetField) and decl.name == 'default'
+            )
+        ):
             anchors['__subject__'] = subject or fq_name
 
         for expr in hard_dep_exprs:


Test patch:
diff --git a/tests/test_schema.py b/tests/test_schema.py
--- a/tests/test_schema.py
+++ b/tests/test_schema.py
@@ -1107,6 +1107,22 @@ def test_schema_refs_03(self):
             })
         )
 
+    def test_schema_refs_04(self):
+        with self.assertRaisesRegex(
+            errors.InvalidReferenceError,
+            "__subject__ cannot be used in this expression",
+        ):
+            self.load_schema(
+                """
+                type User3 {
+                    required property nick: str;
+                    required property name: str {
+                        default := (select __subject__.nick);
+                    };
+                }
+                """
+            )
+
     def test_schema_annotation_inheritance_01(self):
         schema = self.load_schema("""
             abstract annotation noninh;