Skip to content

Fix problems with head analyzer #2680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/src/generator/templates.runtime_renderers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ class _Renderer_Accessor extends RendererBase<Accessor> {
CT_,
() => {
..._Renderer_ModelElement.propertyMap<CT_>(),
'characterLocation': Property(
getValue: (CT_ c) => c.characterLocation,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(
c, remainingNames, 'CharacterLocation'),
isNullValue: (CT_ c) => c.characterLocation == null,
renderValue:
(CT_ c, RendererBase<CT_> r, List<MustachioNode> ast) {
return renderSimple(c.characterLocation, ast, r.template,
parent: r,
getters: _invisibleGetters['CharacterLocation']);
},
),
'definingCombo': Property(
getValue: (CT_ c) => c.definingCombo,
renderVariable:
Expand Down
10 changes: 10 additions & 0 deletions lib/src/model/accessor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ class Accessor extends ModelElement implements EnclosedElement {
[ExecutableMember /*?*/ originalMember])
: super(element, library, packageGraph, originalMember);

@override
CharacterLocation get characterLocation {
if (element.nameOffset < 0) {
assert(element.isSynthetic, 'Invalid offset for non-synthetic element');
// TODO(jcollins-g): switch to [element.nonSynthetic] after analyzer 1.8
return enclosingCombo.characterLocation;
}
return super.characterLocation;
}

@override
PropertyAccessorElement get element => super.element;

Expand Down
7 changes: 7 additions & 0 deletions test/end2end/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ void main() {
inferredTypeSet,
specifiedSet,
untypedMap,
untypedMapWithoutConst,
typedSet;

setUpAll(() async {
Expand All @@ -402,6 +403,8 @@ void main() {
set_literals.constants.firstWhere((v) => v.name == 'specifiedSet');
untypedMap =
set_literals.constants.firstWhere((v) => v.name == 'untypedMap');
untypedMapWithoutConst = set_literals.constants
.firstWhere((v) => v.name == 'untypedMapWithoutConst');
typedSet = set_literals.constants.firstWhere((v) => v.name == 'typedSet');
});

Expand Down Expand Up @@ -431,6 +434,10 @@ void main() {
.toList(),
equals(['int']));
expect(specifiedSet.constantValue, equals('const {}'));
// The analyzer is allowed to return a string with or without leading
// `const` here.
expect(
untypedMapWithoutConst.constantValue, matches(RegExp('(const )?{}')));
expect(untypedMap.modelType.name, equals('Map'));
expect(
(untypedMap.modelType as ParameterizedElementType)
Expand Down
6 changes: 3 additions & 3 deletions testing/test_package/lib/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class aThingToDo {
const aThingToDo(this.who, this.what);
}

const ConstantCat MY_CAT = ConstantCat('tabby');
const List<String> PRETTY_COLORS = <String>[COLOR_GREEN, COLOR_ORANGE, 'blue'];
const ConstantCat MY_CAT = const ConstantCat('tabby');
const List<String> PRETTY_COLORS = const <String>[COLOR_GREEN, COLOR_ORANGE, 'blue'];
@deprecated
int deprecatedField;

Expand Down Expand Up @@ -313,7 +313,7 @@ class Dog implements Cat, E {
static const String aStaticConstField = "A Constant Dog";

/// Verify link substitution in constants (#1535)
static const ShortName aName = ExtendedShortName("hello there");
static const ShortName aName = const ExtendedShortName("hello there");

@protected
final int aProtectedFinalField = 84;
Expand Down
2 changes: 1 addition & 1 deletion testing/test_package/lib/fake.dart
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ class AClassWithFancyProperties {
String aProperty;
}

const _APrivateConstClass CUSTOM_CLASS_PRIVATE = _APrivateConstClass();
const _APrivateConstClass CUSTOM_CLASS_PRIVATE = const _APrivateConstClass();

/// Type inference mixing with anonymous functions.
final importantComputations = {
Expand Down
12 changes: 7 additions & 5 deletions testing/test_package/lib/set_literals.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// @dart=2.9

const inferredTypeSet = {1, 3, 5};
const Set<int> specifiedSet = {};
const untypedMap = {};
const typedSet = <String>{};
const inferredTypeSet = const {1, 3, 5};
const Set<int> specifiedSet = const {};
const untypedMap = const {};
const typedSet = const <String>{};
const untypedMapWithoutConst = {};


class AClassContainingLiterals {
final int value1;
Expand All @@ -12,4 +14,4 @@ class AClassContainingLiterals {
const AClassContainingLiterals(this.value1, this.value2);
}

const aComplexSet = {AClassContainingLiterals(3, 5)};
const aComplexSet = const {const AClassContainingLiterals(3, 5)};