feat(compiler-cli): explain why an expression cannot be used in AOT compilations (#37587)

During AOT compilation, the value of some expressions need to be known at
compile time. The compiler has the ability to statically evaluate expressions
the best it can, but there can be occurrences when an expression cannot be
evaluated statically. For instance, the evaluation could depend on a dynamic
value or syntax is used that the compiler does not understand. Alternatively,
it is possible that an expression could be statically evaluated but the
resulting value would be of an incorrect type.

In these situations, it would be helpful if the compiler could explain why it
is unable to evaluate an expression. To this extend, the static interpreter
in Ivy keeps track of a trail of `DynamicValue`s which follow the path of nodes
that were considered all the way to the node that causes an expression to be
considered dynamic. Up until this commit, this rich trail of information was
not surfaced to a developer so the compiler was of little help to explain
why static evaluation failed, resulting in situations that are hard to debug
and resolve.

This commit adds much more insight to the diagnostic that is produced for static
evaluation errors. For dynamic values, the trail of `DynamicValue` instances
is presented to the user in a meaningful way. If a value is available but not
of the correct type, the type of the resolved value is shown.

Resolves FW-2155

PR Close #37587
This commit is contained in:
JoostK
2020-06-15 12:48:34 +02:00
committed by Andrew Kushnir
parent d2fb552116
commit 712f1bd0b7
14 changed files with 739 additions and 85 deletions

View File

@ -1622,7 +1622,8 @@ runInEachFileSystem(os => {
expect(errors.length).toBe(1);
const {code, messageText} = errors[0];
expect(code).toBe(ngErrorCode(errorCode));
expect(trim(messageText as string)).toContain(errorMessage);
const text = ts.flattenDiagnosticMessageText(messageText, '\n');
expect(trim(text)).toContain(errorMessage);
}
it('should throw if invalid arguments are provided in @NgModule', () => {
@ -3428,8 +3429,11 @@ runInEachFileSystem(os => {
class CompA {}
`);
const errors = env.driveDiagnostics();
expect(errors[0].messageText)
expect(errors.length).toBe(1);
const messageText = ts.flattenDiagnosticMessageText(errors[0].messageText, '\n');
expect(messageText)
.toContain('encapsulation must be a member of ViewEncapsulation enum from @angular/core');
expect(messageText).toContain('Value is of type \'string\'.');
});
it('should handle `changeDetection` field', () => {
@ -3459,9 +3463,12 @@ runInEachFileSystem(os => {
class CompA {}
`);
const errors = env.driveDiagnostics();
expect(errors[0].messageText)
expect(errors.length).toBe(1);
const messageText = ts.flattenDiagnosticMessageText(errors[0].messageText, '\n');
expect(messageText)
.toContain(
'changeDetection must be a member of ChangeDetectionStrategy enum from @angular/core');
expect(messageText).toContain('Value is of type \'string\'.');
});
it('should ignore empty bindings', () => {
@ -4700,7 +4707,10 @@ runInEachFileSystem(os => {
`);
const diags = await driveDiagnostics();
expect(diags[0].messageText).toBe('styleUrls must be an array of strings');
expect(diags.length).toBe(1);
const messageText = ts.flattenDiagnosticMessageText(diags[0].messageText, '\n');
expect(messageText).toContain('styleUrls must be an array of strings');
expect(messageText).toContain('Value is of type \'string\'.');
expect(diags[0].file!.fileName).toBe(absoluteFrom('/test.ts'));
});
});

View File

@ -205,8 +205,10 @@ runInEachFileSystem(() => {
`);
const [error] = env.driveDiagnostics();
expect(error).not.toBeUndefined();
expect(error.messageText).toContain('IsAModule');
expect(error.messageText).toContain('NgModule.imports');
const messageText = ts.flattenDiagnosticMessageText(error.messageText, '\n');
expect(messageText)
.toContain('Value at position 0 in the NgModule.imports of IsAModule is not a class');
expect(messageText).toContain('Value is a reference to \'NotAClass\'.');
expect(error.code).toEqual(ngErrorCode(ErrorCode.VALUE_HAS_WRONG_TYPE));
expect(diagnosticToNode(error, ts.isIdentifier).text).toEqual('NotAClass');
});