| Severity | Error |
| Reported by | Metalama.Framework |
| Message | The source expression '<Expression>' is only available for inspection: it can be read by compile-time code but it cannot be used in generated code. It is already evaluated at its original location, so it may have side effects, and it may reference local variables and parameters that do not exist in the generated code. |
Cause
An ISourceExpression represents an expression of the source code, for instance an argument of a method call. Compile-time code can read its syntax, its text, and its type, and usually it can also use the expression in generated code.
Some expressions are created by Metalama extensions as inspection-only. Compile-time code can inspect them, but it can't emit them, because the original expression is already evaluated at its original location. Evaluating it a second time could repeat its side effects, and it can reference local variables and parameters that don't exist in the generated code.
Metalama reports LAMA0297 when an inspection-only expression is used in generated code, either when a template expands it or when it's converted to text. In the message, <Expression> is the source text of the expression.
Only extension code creates inspection-only expressions, through an API that isn't part of the public aspect API, so this diagnostic can't be shown in an example.
Resolution
Don't emit the expression. Read what you need from it at compile time, for instance its type or its text, and generate new code from this information. If the expression is an argument of a call that you redirect, ask the API that you use to pass the source argument instead of passing the expression yourself.