Skip to content

Commit 97e59ba

Browse files
DanTupCommit Queue
authored andcommitted
[analyzer] Element model fixes
This fixes up some access to `nonSynthetic2` and `enclosingElement2` to avoid exceptions/bugs in the next change to migrate overrides + documentation in the server. Change-Id: Id4260b6388001e179d7d9e1c249ed7251aaa7970 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/391661 Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent d05ea2f commit 97e59ba

File tree

2 files changed

+72
-20
lines changed

2 files changed

+72
-20
lines changed

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 67 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4088,7 +4088,8 @@ class FieldFormalParameterElementImpl2 extends FormalParameterElementImpl
40884088
class FormalParameterElementImpl extends PromotableElementImpl2
40894089
with
40904090
FragmentedAnnotatableElementMixin<FormalParameterFragment>,
4091-
FragmentedElementMixin<FormalParameterFragment>
4091+
FragmentedElementMixin<FormalParameterFragment>,
4092+
_NonTopLevelVariableOrParameter
40924093
implements FormalParameterElement {
40934094
@override
40944095
final ParameterElementImpl firstFragment;
@@ -4108,10 +4109,6 @@ class FormalParameterElementImpl extends PromotableElementImpl2
41084109
// TODO(augmentations): Implement the merge of formal parameters.
41094110
String? get defaultValueCode => firstFragment.defaultValueCode;
41104111

4111-
@override
4112-
Element2? get enclosingElement2 =>
4113-
(firstFragment._enclosingElement3 as Fragment).element;
4114-
41154112
@override
41164113
// TODO(augmentations): Implement the merge of formal parameters.
41174114
List<FormalParameterElement> get formalParameters => firstFragment.parameters
@@ -4196,6 +4193,9 @@ class FormalParameterElementImpl extends PromotableElementImpl2
41964193
// TODO(augmentations): Implement the merge of formal parameters.
41974194
List<TypeParameterElement2> get typeParameters2 => const [];
41984195

4196+
@override
4197+
Element? get _enclosingFunction => firstFragment._enclosingElement3;
4198+
41994199
@override
42004200
T? accept2<T>(ElementVisitor2<T> visitor) {
42014201
return visitor.visitFormalParameterElement(this);
@@ -5003,6 +5003,16 @@ class GetterElementImpl extends ExecutableElementImpl2
50035003
@override
50045004
String? get name3 => firstFragment.name;
50055005

5006+
@override
5007+
Element2 get nonSynthetic2 {
5008+
if (!isSynthetic) {
5009+
return this;
5010+
} else if (variable3 case var variable?) {
5011+
return variable.nonSynthetic2;
5012+
}
5013+
throw StateError('Synthetic getter has no variable');
5014+
}
5015+
50065016
@override
50075017
PropertyInducingElement2? get variable3 => firstFragment.variable2?.element;
50085018

@@ -6455,6 +6465,10 @@ class LocalFunctionElementImpl extends ExecutableElementImpl2
64556465
@override
64566466
String? get documentationComment => _wrappedElement.documentationComment;
64576467

6468+
@override
6469+
// Local functions belong to Fragments, not Elements.
6470+
Element2? get enclosingElement2 => null;
6471+
64586472
@override
64596473
ExecutableFragment get enclosingFunction {
64606474
var element = _wrappedElement.enclosingElement3;
@@ -6549,7 +6563,7 @@ class LocalVariableElementImpl extends NonParameterVariableElementImpl
65496563
}
65506564

65516565
class LocalVariableElementImpl2 extends PromotableElementImpl2
6552-
with WrappedElementMixin
6566+
with WrappedElementMixin, _NonTopLevelVariableOrParameter
65536567
implements LocalVariableElement2 {
65546568
@override
65556569
final LocalVariableElementImpl _wrappedElement;
@@ -6597,6 +6611,9 @@ class LocalVariableElementImpl2 extends PromotableElementImpl2
65976611
return _wrappedElement;
65986612
}
65996613

6614+
@override
6615+
Element? get _enclosingFunction => _wrappedElement.enclosingElement3;
6616+
66006617
@override
66016618
T? accept2<T>(ElementVisitor2<T> visitor) {
66026619
return visitor.visitLocalVariableElement(this);
@@ -9570,6 +9587,16 @@ class SetterElementImpl extends ExecutableElementImpl2
95709587
@override
95719588
String? get name3 => firstFragment.name;
95729589

9590+
@override
9591+
Element2 get nonSynthetic2 {
9592+
if (!isSynthetic) {
9593+
return this;
9594+
} else if (variable3 case var variable?) {
9595+
return variable.nonSynthetic2;
9596+
}
9597+
throw StateError('Synthetic setter has no variable');
9598+
}
9599+
95739600
@override
95749601
PropertyInducingElement2? get variable3 => firstFragment.variable2?.element;
95759602

@@ -10391,7 +10418,8 @@ class TypeParameterElementImpl extends ElementImpl
1039110418
class TypeParameterElementImpl2 extends TypeDefiningElementImpl2
1039210419
with
1039310420
FragmentedAnnotatableElementMixin<TypeParameterFragment>,
10394-
FragmentedElementMixin<TypeParameterFragment>
10421+
FragmentedElementMixin<TypeParameterFragment>,
10422+
_NonTopLevelVariableOrParameter
1039510423
implements TypeParameterElement2 {
1039610424
@override
1039710425
final TypeParameterElementImpl? firstFragment;
@@ -10428,14 +10456,6 @@ class TypeParameterElementImpl2 extends TypeDefiningElementImpl2
1042810456
_syntheticFirstFragment?.bound = _bound;
1042910457
}
1043010458

10431-
@override
10432-
Element2? get enclosingElement2 {
10433-
if (firstFragment case var firstFragment?) {
10434-
return (firstFragment._enclosingElement3 as Fragment).element;
10435-
}
10436-
return null;
10437-
}
10438-
1043910459
TypeParameterElementImpl get firstFragmentOrSynthetic {
1044010460
return firstFragment ??
1044110461
(_syntheticFirstFragment ??= TypeParameterElementImpl(name3, -1)
@@ -10449,6 +10469,9 @@ class TypeParameterElementImpl2 extends TypeDefiningElementImpl2
1044910469
@override
1045010470
LibraryElement2 get library2 => super.library2!;
1045110471

10472+
@override
10473+
Element? get _enclosingFunction => firstFragment?._enclosingElement3;
10474+
1045210475
@override
1045310476
T? accept2<T>(ElementVisitor2<T> visitor) {
1045410477
return visitor.visitTypeParameterElement(this);
@@ -10726,6 +10749,34 @@ mixin _HasLibraryMixin on ElementImpl {
1072610749
Source get source => enclosingElement3!.source!;
1072710750
}
1072810751

10752+
mixin _NonTopLevelVariableOrParameter on Element2 {
10753+
@override
10754+
Element2? get enclosingElement2 {
10755+
// TODO(dantup): Can we simplify this code and inline it into each class?
10756+
10757+
var enclosingFunction = _enclosingFunction;
10758+
return switch (enclosingFunction) {
10759+
// There is no enclosingElement for a local function so we need to
10760+
// determine whether our enclosing FunctionElementImpl is a local function
10761+
// or not.
10762+
// TODO(dantup): Is the real issue here that we're getting
10763+
// FunctionElementImpl here that should be LocalFunctionElementImpl?
10764+
FunctionElementImpl()
10765+
when enclosingFunction.enclosingElement3 is ExecutableElementImpl ||
10766+
enclosingFunction.enclosingElement3 is VariableElementImpl =>
10767+
null,
10768+
// GenericFunctionTypeElementImpl currently implements Fragment but throws
10769+
// if we try to access `element`.
10770+
GenericFunctionTypeElementImpl() => null,
10771+
// Otherwise, we have a valid enclosing element.
10772+
Fragment(:var element) => element,
10773+
_ => null,
10774+
};
10775+
}
10776+
10777+
Element? get _enclosingFunction;
10778+
}
10779+
1072910780
/// Instances of [List]s that are used as "not yet computed" values, they
1073010781
/// must be not `null`, and not identical to `const <T>[]`.
1073110782
class _Sentinel {
@@ -10743,10 +10794,7 @@ class _Sentinel {
1074310794

1074410795
extension on Fragment {
1074510796
/// The content of the documentation comment (including delimiters) for this
10746-
/// element or fragment.
10747-
///
10748-
/// If the receiver is an element that has fragments, the comment will be a
10749-
/// concatenation of the comments from all of the fragments.
10797+
/// fragment.
1075010798
///
1075110799
/// Returns `null` if the receiver does not have or does not support
1075210800
/// documentation.

pkg/analyzer/lib/src/utilities/extensions/element.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ extension ElementImplExtension on ElementImpl {
132132
extension ElementOrNullExtension on Element? {
133133
Element2? get asElement2 {
134134
var self = this;
135-
if (self is DynamicElementImpl) {
135+
if (self == null) {
136+
return null;
137+
} else if (self is DynamicElementImpl) {
136138
return self;
137139
} else if (self is FunctionElementImpl &&
138140
self.enclosingElement3 is! CompilationUnitElement) {
@@ -142,6 +144,8 @@ extension ElementOrNullExtension on Element? {
142144
return self.element;
143145
} else if (self is LabelElementImpl) {
144146
return self.element2;
147+
} else if (self is LibraryElementImpl) {
148+
return self;
145149
} else if (self is LocalVariableElementImpl) {
146150
return self.element2;
147151
} else if (self is MultiplyDefinedElementImpl) {

0 commit comments

Comments
 (0)