Skip to content

Commit a73b0ce

Browse files
[vm, lib] Teach mirrors about the Never type and member signatures.
Bug: #12478 Bug: #40497 Bug: #40510 Change-Id: I841d7e239b8235555ec26fbcb74ca41b5de60f58 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/134806 Commit-Queue: Ryan Macnak <[email protected]> Reviewed-by: Alexander Markov <[email protected]>
1 parent 05b806a commit a73b0ce

File tree

12 files changed

+65
-24
lines changed

12 files changed

+65
-24
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ used (see Issue [39627][]).
2222
options. Previously setting a socket options would be ignored and getting a
2323
socket option would return `null`.
2424

25+
#### `dart:mirrors`
26+
27+
* Added `MirrorSystem.neverType`.
28+
2529
### Dart VM
2630

2731
### Tools

runtime/lib/mirrors.cc

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ static RawInstance* CreateMethodMirror(const Function& func,
272272
<< Mirrors::kFactoryCtor);
273273
kind_flags |=
274274
(static_cast<intptr_t>(func.is_external()) << Mirrors::kExternal);
275-
bool is_synthetic = func.is_no_such_method_forwarder();
275+
bool is_synthetic = func.is_synthetic();
276276
kind_flags |= (static_cast<intptr_t>(is_synthetic) << Mirrors::kSynthetic);
277277
kind_flags |= (static_cast<intptr_t>(func.is_extension_member())
278278
<< Mirrors::kExtensionMember);
@@ -311,7 +311,9 @@ static RawInstance* CreateClassMirror(const Class& cls,
311311
ASSERT(ref_type.IsCanonical());
312312
return CreateClassMirror(cls, ref_type, is_declaration, owner_mirror);
313313
}
314-
ASSERT(!cls.IsDynamicClass() && !cls.IsVoidClass());
314+
ASSERT(!cls.IsDynamicClass());
315+
ASSERT(!cls.IsVoidClass());
316+
ASSERT(!cls.IsNeverClass());
315317
ASSERT(!type.IsNull());
316318
ASSERT(type.IsFinalized());
317319

@@ -625,6 +627,10 @@ static RawInstance* CreateTypeMirror(const AbstractType& type) {
625627
Array& args = Array::Handle(Array::New(1));
626628
args.SetAt(0, Symbols::Dynamic());
627629
return CreateMirror(Symbols::_SpecialTypeMirror(), args);
630+
} else if (cls.IsNeverClass()) {
631+
Array& args = Array::Handle(Array::New(1));
632+
args.SetAt(0, Symbols::Never());
633+
return CreateMirror(Symbols::_SpecialTypeMirror(), args);
628634
}
629635
// TODO(regis): Until mirrors reflect nullability, force kLegacy, except for
630636
// Null type, which should remain nullable.
@@ -830,7 +836,8 @@ DEFINE_NATIVE_ENTRY(Mirrors_makeLocalClassMirror, 0, 1) {
830836
ASSERT(type.HasTypeClass());
831837
const Class& cls = Class::Handle(type.type_class());
832838
ASSERT(!cls.IsNull());
833-
if (cls.IsDynamicClass() || cls.IsVoidClass() || cls.IsTypedefClass()) {
839+
if (cls.IsDynamicClass() || cls.IsVoidClass() || cls.IsNeverClass() ||
840+
cls.IsTypedefClass()) {
834841
Exceptions::ThrowArgumentError(type);
835842
UNREACHABLE();
836843
}
@@ -1172,8 +1179,7 @@ DEFINE_NATIVE_ENTRY(LibraryMirror_members, 0, 2) {
11721179
entry = entries.GetNext();
11731180
if (entry.IsClass()) {
11741181
const Class& klass = Class::Cast(entry);
1175-
// We filter out dynamic.
1176-
// TODO(12478): Should not need to filter out dynamic.
1182+
ASSERT(!klass.IsVoidClass() && !klass.IsNeverClass());
11771183
if (!klass.IsDynamicClass()) {
11781184
error = klass.EnsureIsFinalized(thread);
11791185
if (!error.IsNull()) {
@@ -1700,6 +1706,8 @@ DEFINE_NATIVE_ENTRY(DeclarationMirror_location, 0, 1) {
17001706
ASSERT(!script.IsNull());
17011707
const String& uri = String::Handle(zone, script.url());
17021708
return CreateSourceLocation(uri, 1, 1);
1709+
} else {
1710+
FATAL1("Unexpected declaration type: %s", decl.ToCString());
17031711
}
17041712

17051713
ASSERT(!script.IsNull());

runtime/vm/compiler/frontend/bytecode_reader.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,8 +2261,7 @@ void BytecodeReaderHelper::ReadFunctionDeclarations(const Class& cls) {
22612261
function.set_is_declared_in_bytecode(true);
22622262
function.set_has_pragma(has_pragma);
22632263
function.set_end_token_pos(end_position);
2264-
function.set_is_no_such_method_forwarder(
2265-
(flags & kIsNoSuchMethodForwarderFlag) != 0);
2264+
function.set_is_synthetic((flags & kIsNoSuchMethodForwarderFlag) != 0);
22662265
function.set_is_reflectable((flags & kIsReflectableFlag) != 0);
22672266
function.set_is_debuggable((flags & kIsDebuggableFlag) != 0);
22682267
function.set_is_extension_member(is_extension_member);

runtime/vm/compiler/frontend/kernel_translation_helper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ class ProcedureHelper {
545545
kRedirectingFactoryConstructor = 1 << 6,
546546
kNoSuchMethodForwarder = 1 << 7,
547547
kExtensionMember = 1 << 8,
548+
kMemberSignature = 1 << 9,
548549
};
549550

550551
explicit ProcedureHelper(KernelReaderHelper* helper)
@@ -571,6 +572,7 @@ class ProcedureHelper {
571572
return (flags_ & kNoSuchMethodForwarder) != 0;
572573
}
573574
bool IsExtensionMember() const { return (flags_ & kExtensionMember) != 0; }
575+
bool IsMemberSignature() const { return (flags_ & kMemberSignature) != 0; }
574576

575577
NameIndex canonical_name_;
576578
TokenPosition start_position_;

runtime/vm/kernel_loader.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,8 +1917,8 @@ void KernelLoader::LoadProcedure(const Library& library,
19171917
script_class, procedure_helper.start_position_));
19181918
function.set_has_pragma(has_pragma_annotation);
19191919
function.set_end_token_pos(procedure_helper.end_position_);
1920-
function.set_is_no_such_method_forwarder(
1921-
procedure_helper.IsNoSuchMethodForwarder());
1920+
function.set_is_synthetic(procedure_helper.IsNoSuchMethodForwarder() ||
1921+
procedure_helper.IsMemberSignature());
19221922
if (register_function) {
19231923
functions_.Add(&function);
19241924
} else {

runtime/vm/object.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7892,7 +7892,7 @@ RawFunction* Function::New(const String& name,
78927892
result.set_is_generated_body(false);
78937893
result.set_has_pragma(false);
78947894
result.set_is_polymorphic_target(false);
7895-
result.set_is_no_such_method_forwarder(false);
7895+
result.set_is_synthetic(false);
78967896
NOT_IN_PRECOMPILED(result.set_state_bits(0));
78977897
result.set_owner(owner);
78987898
NOT_IN_PRECOMPILED(result.set_token_pos(token_pos));

runtime/vm/object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3395,7 +3395,7 @@ class Function : public Object {
33953395
V(GeneratedBody, is_generated_body) \
33963396
V(PolymorphicTarget, is_polymorphic_target) \
33973397
V(HasPragma, has_pragma) \
3398-
V(IsNoSuchMethodForwarder, is_no_such_method_forwarder) \
3398+
V(IsSynthetic, is_synthetic) \
33993399
V(IsExtensionMember, is_extension_member)
34003400

34013401
#define DEFINE_ACCESSORS(name, accessor_name) \

runtime/vm/source_report.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ bool SourceReport::ShouldSkipFunction(const Function& func) {
105105
return true;
106106
}
107107
if (func.is_abstract() || func.IsImplicitConstructor() ||
108-
func.IsRedirectingFactory() || func.is_no_such_method_forwarder()) {
108+
func.IsRedirectingFactory() || func.is_synthetic()) {
109109
return true;
110110
}
111111
// Note that context_scope() remains null for closures declared in bytecode,

sdk/lib/_internal/vm/lib/mirrors_impl.dart

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ bool _subtypeTest(Type a, Type b) native 'TypeMirror_subtypeTest';
8484
class _MirrorSystem extends MirrorSystem {
8585
final TypeMirror dynamicType = new _SpecialTypeMirror._('dynamic');
8686
final TypeMirror voidType = new _SpecialTypeMirror._('void');
87+
final TypeMirror neverType = new _SpecialTypeMirror._('Never');
8788

8889
var _libraries;
8990
Map<Uri, LibraryMirror> get libraries {
@@ -685,13 +686,15 @@ class _ClassMirror extends _ObjectMirror implements ClassMirror, _TypeMirror {
685686

686687
bool isSubtypeOf(TypeMirror other) {
687688
if (other == currentMirrorSystem().dynamicType) return true;
688-
if (other == currentMirrorSystem().voidType) return false;
689+
if (other == currentMirrorSystem().voidType) return true;
690+
if (other == currentMirrorSystem().neverType) return false;
689691
return _subtypeTest(_reflectedType, (other as _TypeMirror)._reflectedType);
690692
}
691693

692694
bool isAssignableTo(TypeMirror other) {
693695
if (other == currentMirrorSystem().dynamicType) return true;
694-
if (other == currentMirrorSystem().voidType) return false;
696+
if (other == currentMirrorSystem().voidType) return true;
697+
if (other == currentMirrorSystem().neverType) return false;
695698
final otherReflectedType = (other as _TypeMirror)._reflectedType;
696699
return _subtypeTest(_reflectedType, otherReflectedType) ||
697700
_subtypeTest(otherReflectedType, _reflectedType);
@@ -893,13 +896,15 @@ class _TypeVariableMirror extends _DeclarationMirror
893896

894897
bool isSubtypeOf(TypeMirror other) {
895898
if (other == currentMirrorSystem().dynamicType) return true;
896-
if (other == currentMirrorSystem().voidType) return false;
899+
if (other == currentMirrorSystem().voidType) return true;
900+
if (other == currentMirrorSystem().neverType) return false;
897901
return _subtypeTest(_reflectedType, (other as _TypeMirror)._reflectedType);
898902
}
899903

900904
bool isAssignableTo(TypeMirror other) {
901905
if (other == currentMirrorSystem().dynamicType) return true;
902-
if (other == currentMirrorSystem().voidType) return false;
906+
if (other == currentMirrorSystem().voidType) return true;
907+
if (other == currentMirrorSystem().neverType) return false;
903908
final otherReflectedType = (other as _TypeMirror)._reflectedType;
904909
return _subtypeTest(_reflectedType, otherReflectedType) ||
905910
_subtypeTest(otherReflectedType, _reflectedType);
@@ -995,13 +1000,15 @@ class _TypedefMirror extends _DeclarationMirror
9951000

9961001
bool isSubtypeOf(TypeMirror other) {
9971002
if (other == currentMirrorSystem().dynamicType) return true;
998-
if (other == currentMirrorSystem().voidType) return false;
1003+
if (other == currentMirrorSystem().voidType) return true;
1004+
if (other == currentMirrorSystem().neverType) return false;
9991005
return _subtypeTest(_reflectedType, (other as _TypeMirror)._reflectedType);
10001006
}
10011007

10021008
bool isAssignableTo(TypeMirror other) {
10031009
if (other == currentMirrorSystem().dynamicType) return true;
1004-
if (other == currentMirrorSystem().voidType) return false;
1010+
if (other == currentMirrorSystem().voidType) return true;
1011+
if (other == currentMirrorSystem().neverType) return false;
10051012
final otherReflectedType = (other as _TypeMirror)._reflectedType;
10061013
return _subtypeTest(_reflectedType, otherReflectedType) ||
10071014
_subtypeTest(otherReflectedType, _reflectedType);

sdk/lib/mirrors/mirrors.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
library dart.mirrors;
6161

6262
import 'dart:async' show Future;
63+
import "dart:_internal" show Since;
6364

6465
/**
6566
* A [MirrorSystem] is the main interface used to reflect on a set of
@@ -110,6 +111,12 @@ abstract class MirrorSystem {
110111
*/
111112
TypeMirror get voidType;
112113

114+
/**
115+
* A mirror on the [:Never:] type.
116+
*/
117+
@Since("2.8")
118+
TypeMirror get neverType;
119+
113120
/**
114121
* Returns the name of [symbol].
115122
*

sdk_nnbd/lib/_internal/vm/lib/mirrors_impl.dart

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ bool _subtypeTest(Type a, Type b) native 'TypeMirror_subtypeTest';
8282
class _MirrorSystem extends MirrorSystem {
8383
final TypeMirror dynamicType = new _SpecialTypeMirror._('dynamic');
8484
final TypeMirror voidType = new _SpecialTypeMirror._('void');
85+
final TypeMirror neverType = new _SpecialTypeMirror._('Never');
8586

8687
var _libraries;
8788
Map<Uri, LibraryMirror> get libraries {
@@ -682,13 +683,15 @@ class _ClassMirror extends _ObjectMirror implements ClassMirror, _TypeMirror {
682683

683684
bool isSubtypeOf(TypeMirror other) {
684685
if (other == currentMirrorSystem().dynamicType) return true;
685-
if (other == currentMirrorSystem().voidType) return false;
686+
if (other == currentMirrorSystem().voidType) return true;
687+
if (other == currentMirrorSystem().neverType) return false;
686688
return _subtypeTest(_reflectedType, (other as _TypeMirror)._reflectedType);
687689
}
688690

689691
bool isAssignableTo(TypeMirror other) {
690692
if (other == currentMirrorSystem().dynamicType) return true;
691-
if (other == currentMirrorSystem().voidType) return false;
693+
if (other == currentMirrorSystem().voidType) return true;
694+
if (other == currentMirrorSystem().neverType) return false;
692695
final otherReflectedType = (other as _TypeMirror)._reflectedType;
693696
return _subtypeTest(_reflectedType, otherReflectedType) ||
694697
_subtypeTest(otherReflectedType, _reflectedType);
@@ -890,13 +893,15 @@ class _TypeVariableMirror extends _DeclarationMirror
890893

891894
bool isSubtypeOf(TypeMirror other) {
892895
if (other == currentMirrorSystem().dynamicType) return true;
893-
if (other == currentMirrorSystem().voidType) return false;
896+
if (other == currentMirrorSystem().voidType) return true;
897+
if (other == currentMirrorSystem().neverType) return false;
894898
return _subtypeTest(_reflectedType, (other as _TypeMirror)._reflectedType);
895899
}
896900

897901
bool isAssignableTo(TypeMirror other) {
898902
if (other == currentMirrorSystem().dynamicType) return true;
899-
if (other == currentMirrorSystem().voidType) return false;
903+
if (other == currentMirrorSystem().voidType) return true;
904+
if (other == currentMirrorSystem().neverType) return false;
900905
final otherReflectedType = (other as _TypeMirror)._reflectedType;
901906
return _subtypeTest(_reflectedType, otherReflectedType) ||
902907
_subtypeTest(otherReflectedType, _reflectedType);
@@ -992,13 +997,15 @@ class _TypedefMirror extends _DeclarationMirror
992997

993998
bool isSubtypeOf(TypeMirror other) {
994999
if (other == currentMirrorSystem().dynamicType) return true;
995-
if (other == currentMirrorSystem().voidType) return false;
1000+
if (other == currentMirrorSystem().voidType) return true;
1001+
if (other == currentMirrorSystem().neverType) return false;
9961002
return _subtypeTest(_reflectedType, (other as _TypeMirror)._reflectedType);
9971003
}
9981004

9991005
bool isAssignableTo(TypeMirror other) {
10001006
if (other == currentMirrorSystem().dynamicType) return true;
1001-
if (other == currentMirrorSystem().voidType) return false;
1007+
if (other == currentMirrorSystem().voidType) return true;
1008+
if (other == currentMirrorSystem().neverType) return false;
10021009
final otherReflectedType = (other as _TypeMirror)._reflectedType;
10031010
return _subtypeTest(_reflectedType, otherReflectedType) ||
10041011
_subtypeTest(otherReflectedType, _reflectedType);

sdk_nnbd/lib/mirrors/mirrors.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
library dart.mirrors;
5959

6060
import 'dart:async' show Future;
61+
import "dart:_internal" show Since;
6162

6263
/**
6364
* A [MirrorSystem] is the main interface used to reflect on a set of
@@ -108,6 +109,12 @@ abstract class MirrorSystem {
108109
*/
109110
TypeMirror get voidType;
110111

112+
/**
113+
* A mirror on the [:Never:] type.
114+
*/
115+
@Since("2.8")
116+
TypeMirror get neverType;
117+
111118
/**
112119
* Returns the name of [symbol].
113120
*

0 commit comments

Comments
 (0)