Repository: edgedb/edgedb

Issue:
UDF InvalidReferenceError on inherited type
I understand 1.0 has not been officially announced but it was available for upgrade via cli.

- EdgeDB CLI Version: EdgeDB CLI 1.0.0+7b66089
- EdgeDB Instance Version: 1.0+9236fa8
- OS Version: macosx

Steps to Reproduce:

1. issue `edgedb migration create` with the schema shown in the `schema` section
2. observe error 'edgedb error: InvalidReferenceError: object type 'default::Entity' has no link or property 'name''

__optionally__

1. comment out the UDF function `create_project`
2. issue `edgedb migration create` with the schema shown in the `schema` section
3. observe successful migration creation
4. issue `edgedb migration apply`
5. observe successfull migration application
4. issue the following query below and observe successful execution:

```
with
	name := "my project",
	project := (
		insert Project {
			name := name,
		}
	),
	event := (
		insert ProjectCreatedEvent {
			entity := project,
			name := name,
		}
	),
select event {
	id,
	typename,
	emittedAt,
	name,
	entity: {
		id,
		typename,					
	}
}
{default::ProjectCreatedEvent {id: dfa460ee-8a77-11ec-aca2-5fc77c199fd0, typename: 'default::ProjectCreatedEvent', emittedAt: <datetime>'2022-02-10T13:46:39.947598Z', name: 'my project', entity: default::Project {id: dfa450c2-8a77-11ec-aca2-eff5c502242d, typename: 'default::Project'}}}
```

Schema:

```
module default {
	abstract type Base {
		property typename := .__type__.name;
	}

	abstract type Entity extending Base {
	}

	abstract type Event extending Base {
		required link entity -> Entity;
		required property emittedAt -> datetime {
			default := datetime_current()
		};
	}

	type Project extending Entity {
		required property name -> str {
			constraint exclusive;
		};
	}

	type ProjectCreatedEvent extending Event {
		required property name -> str;
	}

	function create_project(name: str) -> ProjectCreatedEvent {
		using (
			with
				project := (
					insert Project {
						name := name,
					}
				),
				event := (
					insert ProjectCreatedEvent {
						entity := project,
						name := name,
					}
				),
			select event {
				id,
				typename,
				emittedAt,
				entity: {
					id,
					typename,
					name,
				}
			}
		)
	}
}
```

Aside from the failure to create the migration, it would be helpful to know where in the schema the reference error was thrown, as the error does not provide that context. Thanks!



Discussion:
After more fiddling this may be a non issue. removing the `name` property from the resulting event shaping select results in 

```
error: data-modifying statements are not allowed in function bodies
```

So is this use case not permitted inside UDFs?
Sorry I missed this one:

Yeah, DML is not yet permitted in function bodies.
The `name` property in the `entity` event is an error, since Entity doesn't have name.

But it's a bug that we don't show the source location!

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
@@ -578,7 +578,8 @@ def _normalize_view_ptr_expr(
 
     if compexpr is None:
         ptrcls = setgen.resolve_ptr(
-            ptrsource, ptrname, track_ref=lexpr, ctx=ctx)
+            ptrsource, ptrname, track_ref=lexpr, ctx=ctx,
+            source_context=shape_el.context)
         if is_polymorphic:
             ptrcls = schemactx.derive_ptr(
                 ptrcls, view_scls, ctx=ctx)


Test patch:
diff --git a/tests/test_edgeql_select.py b/tests/test_edgeql_select.py
--- a/tests/test_edgeql_select.py
+++ b/tests/test_edgeql_select.py
@@ -6304,6 +6304,18 @@ async def test_edgeql_select_duplicate_definition_03(self):
                 }
             """)
 
+    async def test_edgeql_select_missing_shape_field(self):
+        with self.assertRaisesRegex(
+            edgedb.InvalidReferenceError,
+            "has no link or property",
+            _position=51,
+        ):
+            await self.con.execute("""
+                SELECT User {
+                    missing,
+                }
+            """)
+
     async def test_edgeql_select_big_set_literal(self):
         res = await self.con.query("""
             SELECT {