Skip to content

thisOrAncestorOfType2() behaves unexpectedly on MixinElement2 #56917

@DanTup

Description

@DanTup

While migrating some code, I got MixinElement2 like this:

MixinDeclaration x;
var element = x.declaredFragment.element;

The runtimeType is NotAugmentedMixinElementImpl. I passed it to some shared code calls thisOrAncestorOrType2<InterfaceElement2>() which I expected to return itself, however it throws:

Unsupported operation: Cannot get an enclosingElement2 for a fragment
#0      ElementImpl.enclosingElement2 (package:analyzer/src/dart/element/element.dart:2449:7)
#1      ElementImpl.thisOrAncestorOfType2 (package:analyzer/src/dart/element/element.dart:3092:30)
#2      MaybeAugmentedInstanceElementMixin.thisOrAncestorOfType2 (package:analyzer/src/dart/element/element.dart:7149:19)
#3      DartUnitHoverComputer.Eval ()

If I evaluate element is InterfaceElement2 I get true, so I would have expected it to return itself before even looking at any ancestors (but if it did look at ancestors, I would never expect it to get to a Fragment). The implementation of MaybeAugmentedInstanceElementMixin.thisOrAncestorOfType2 seems to go to .declaration which doesn't seem correct (at least not in this case where I expect to be entirely in the Element2 model with no Fragments).

(Some more discussion about this issue was on Discord)

Metadata

Metadata

Assignees

Labels

P1A high priority bug; for example, a single project is unusable or has many test failureslegacy-area-analyzerUse area-devexp instead.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions