Skip to content

Commit c9d6986

Browse files
kallentuCommit Queue
authored andcommitted
[analyzer] Revert 'Fix broken code generators in g3.' and add new fix for code generators that result in InvalidTypes.
Reverting previous change since it does break some other parts of g3. Shortcircuiting the `??` isn't sufficient in some cases. So we attempt to fix it further downstream. This change does a check for unresolved types and ignores the cast since we can't tell what type it is anyways. Similar to what we do with type parameters and when we don't know the type. The generators in g3 seem to emit a lot more InvalidTypes and this is necessary to avoid the errors now that we propagate errors upwards. b/293326927 Change-Id: I172980dfd3338f27d0a57e156b15dd937619ff73 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/316841 Commit-Queue: Kallen Tu <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]>
1 parent 7d6414a commit c9d6986

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

pkg/analyzer/lib/src/dart/constant/evaluation.dart

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2026,11 +2026,6 @@ class DartObjectComputer {
20262026
Constant lazyQuestionQuestion(Expression node, DartObjectImpl leftOperand,
20272027
Constant Function() rightOperandComputer) {
20282028
if (leftOperand.isNull) {
2029-
// TODO(kallentu): Remove this once we have a better representation for
2030-
// unresolved types.
2031-
if (leftOperand.isInvalid) {
2032-
return leftOperand;
2033-
}
20342029
return rightOperandComputer();
20352030
}
20362031
return leftOperand;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analyzer/dart/element/type.dart';
6+
import 'package:analyzer/src/dart/element/type_visitor.dart';
7+
8+
/// Return `true` if the [type] has an [InvalidType].
9+
bool hasInvalidType(DartType type) {
10+
var visitor = _InvalidTypeVisitor();
11+
type.accept(visitor);
12+
return visitor.result;
13+
}
14+
15+
/// A visitor to find if a type contains any [InvalidType]s.
16+
///
17+
/// To find the result, check [result] on this instance after visiting the tree.
18+
///
19+
/// The actual value returned by the visit methods is merely used so that
20+
/// [RecursiveTypeVisitor] stops visiting the type once the first type parameter
21+
/// type is found.
22+
class _InvalidTypeVisitor extends RecursiveTypeVisitor {
23+
/// The result of whether any [InvalidType]s were found.
24+
bool result = false;
25+
26+
@override
27+
bool visitDartType(DartType dartType) {
28+
if (dartType is InvalidType) {
29+
result = true;
30+
return false;
31+
}
32+
return true;
33+
}
34+
}

pkg/analyzer/lib/src/dart/constant/value.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import 'package:analyzer/dart/element/type.dart';
1515
import 'package:analyzer/dart/element/type_provider.dart';
1616
import 'package:analyzer/diagnostic/diagnostic.dart';
1717
import 'package:analyzer/error/error.dart';
18+
import 'package:analyzer/src/dart/constant/has_invalid_type.dart';
1819
import 'package:analyzer/src/dart/constant/has_type_parameter_reference.dart';
1920
import 'package:analyzer/src/dart/element/element.dart';
2021
import 'package:analyzer/src/dart/element/extensions.dart';
@@ -306,6 +307,14 @@ class DartObjectImpl implements DartObject, Constant {
306307
return this;
307308
}
308309

310+
// If any type is unresolved, we cannot prove that the cast will fail.
311+
if (isInvalid ||
312+
castType.isInvalid ||
313+
hasInvalidType(type) ||
314+
hasInvalidType(resultType)) {
315+
return this;
316+
}
317+
309318
// We don't know the actual value of a type parameter.
310319
// So, the object type might be a subtype of the result type.
311320
if (hasTypeParameterReference(resultType)) {

0 commit comments

Comments
 (0)