Repository: edgedb/edgedb

Issue:
ERROR: InternalServerError: ObjectRef expected (got type 'AnyType')
<!-- Please search existing issues to avoid creating duplicates. -->
- EdgeDB Version: edgedb-cli 1.0.0-alpha.5
- OS Version: Standard Linux

Steps to Reproduce:

1. Create a migration like this (where a field has the type of `anytype`);
```
START MIGRATION TO {
    module default {
        type ObjectX {
            required property value -> anytype;
        }
    }
};
POPULATE MIGRATION;
COMMIT MIGRATION;
```

<!-- If the issue is about a query error, please also provide your schema -->
Schema:
```traceback
ERROR: InternalServerError: ObjectRef expected (got type 'AnyType')
  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 "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha5/lib/python3.8/site-packages/edb/server/procpool/worker.py", line 75, in worker
          res = await meth(*args)
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha5/lib/python3.8/site-packages/edb/server/compiler/compiler.py", line 1795, in compile_eql_tokens
          return self._compile(ctx=ctx, tokens=eql_tokens)
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha5/lib/python3.8/site-packages/edb/server/compiler/compiler.py", line 1429, in _compile
          comp: dbstate.BaseQuery = self._compile_dispatch_ql(ctx, stmt)
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha5/lib/python3.8/site-packages/edb/server/compiler/compiler.py", line 1352, in _compile_dispatch_ql
          return self._compile_ql_migration(ctx, ql)
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha5/lib/python3.8/site-packages/edb/server/compiler/compiler.py", line 789, in _compile_ql_migration
          target_schema = s_ddl.apply_sdl(
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha5/lib/python3.8/site-packages/edb/schema/ddl.py", line 346, in apply_sdl
          ddl_stmts = s_decl.sdl_to_ddl(current_schema, documents)
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha5/lib/python3.8/site-packages/edb/edgeql/declarative.py", line 209, in sdl_to_ddl
          trace_layout(decl_ast, ctx=ctx)
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha5/lib/python3.8/functools.py", line 875, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha5/lib/python3.8/site-packages/edb/edgeql/declarative.py", line 266, in trace_layout_CreateObjectType
          _trace_item_layout(node, ctx=ctx)
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha5/lib/python3.8/site-packages/edb/edgeql/declarative.py", line 325, in _trace_item_layout
          target = _resolve_type_expr(decl.target, ctx=ctx)
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha5/lib/python3.8/site-packages/edb/edgeql/declarative.py", line 747, in _resolve_type_expr
          refname = ctx.get_ref_name(texpr.maintype)
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha5/lib/python3.8/site-packages/edb/edgeql/declarative.py", line 105, in get_ref_name
          raise TypeError(
      TypeError: ObjectRef expected (got type 'AnyType')
```


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
@@ -1221,7 +1221,14 @@ def _get_local_obj(
 
     obj = ctx.objects.get(refname)
 
-    if obj is not None and not isinstance(obj, tracer_type):
+    if isinstance(obj, s_pseudo.PseudoType):
+        raise errors.SchemaError(
+            f'invalid type: {obj.get_verbosename(ctx.schema)} is a generic '
+            f'type and they are not supported in user-defined schema',
+            context=sourcectx,
+        )
+
+    elif obj is not None and not isinstance(obj, tracer_type):
         obj_type = TRACER_TO_REAL_TYPE_MAP[type(obj)]
         real_type = TRACER_TO_REAL_TYPE_MAP[tracer_type]
         raise errors.InvalidReferenceError(
diff --git a/edb/pgsql/delta.py b/edb/pgsql/delta.py
--- a/edb/pgsql/delta.py
+++ b/edb/pgsql/delta.py
@@ -2099,7 +2099,7 @@ def _undo_everything(
         for prop, new_typ in props:
             try:
                 cmd.add(new_typ.as_create_delta(schema))
-            except NotImplementedError:
+            except errors.UnsupportedFeatureError:
                 pass
 
             delta_alter, cmd_alter, alter_context = prop.init_delta_branch(
diff --git a/edb/schema/types.py b/edb/schema/types.py
--- a/edb/schema/types.py
+++ b/edb/schema/types.py
@@ -532,7 +532,12 @@ def as_create_delta(
         view_name: Optional[s_name.QualName] = None,
         attrs: Optional[Dict[str, Any]] = None,
     ) -> sd.Command:
-        raise NotImplementedError
+        raise errors.UnsupportedFeatureError(
+            f'unsupported type intersection in schema',
+            hint=f'Type intersections are currently '
+                 f'unsupported as valid link targets.',
+            context=self.sourcectx,
+        )
 
 
 class TypeExprShell(TypeShell[TypeT_co]):


Test patch:
diff --git a/tests/test_edgeql_ddl.py b/tests/test_edgeql_ddl.py
--- a/tests/test_edgeql_ddl.py
+++ b/tests/test_edgeql_ddl.py
@@ -1963,6 +1963,22 @@ async def test_edgeql_ddl_link_target_bad_03(self):
                 ALTER TYPE Foo ALTER PROPERTY b RESET TYPE;
             ''')
 
+    async def test_edgeql_ddl_link_target_bad_04(self):
+        await self.con.execute('''
+            CREATE TYPE Foo;
+            CREATE TYPE Bar;
+        ''')
+
+        with self.assertRaisesRegex(
+            edgedb.UnsupportedFeatureError,
+            "unsupported type intersection in schema"
+        ):
+            await self.con.execute('''
+                CREATE TYPE Spam {
+                    CREATE MULTI LINK foobar := Foo[IS Bar]
+                };
+            ''')
+
     async def test_edgeql_ddl_link_target_merge_01(self):
         await self.con.execute('''
 
diff --git a/tests/test_schema.py b/tests/test_schema.py
--- a/tests/test_schema.py
+++ b/tests/test_schema.py
@@ -464,6 +464,44 @@ def test_schema_bad_type_10(self):
             type Foo3 extending Foo0;
         """
 
+    @tb.must_fail(errors.UnsupportedFeatureError,
+                  "unsupported type intersection in schema")
+    def test_schema_bad_type_11(self):
+        """
+            type Foo;
+            type Bar;
+            type Spam {
+                multi link foobar := Foo[IS Bar];
+            }
+        """
+
+    @tb.must_fail(errors.SchemaError,
+                  "invalid type: pseudotype 'anytype' is a generic type")
+    def test_schema_bad_type_12(self):
+        """
+            type Foo {
+                property val -> anytype;
+            }
+        """
+
+    @tb.must_fail(errors.SchemaError,
+                  "invalid type: pseudotype 'anytype' is a generic type")
+    def test_schema_bad_type_13(self):
+        """
+            type Foo {
+                link val -> anytype;
+            }
+        """
+
+    @tb.must_fail(errors.SchemaError,
+                  "invalid type: pseudotype 'anytuple' is a generic type")
+    def test_schema_bad_type_14(self):
+        """
+            type Foo {
+                property val -> anytuple;
+            }
+        """
+
     def test_schema_computable_cardinality_inference_01(self):
         schema = self.load_schema("""
             type Object {