Skip to content

Commit 9b75b24

Browse files
committed
AbstractClassInstantiationError beats NoSuchMethodError
When both errors are determined at compile time, AbstractClassInstantiationError must win. [email protected] Review URL: https://codereview.chromium.org/1499793002 .
1 parent 767b57b commit 9b75b24

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

pkg/compiler/lib/src/ssa/builder.dart

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5134,15 +5134,17 @@ class SsaBuilder extends ast.Visitor
51345134
stack.add(graph.addConstantNull(compiler));
51355135
return;
51365136
}
5137+
51375138
if (checkTypeVariableBounds(node, type)) return;
51385139

5139-
var inputs = <HInstruction>[];
5140-
if (constructor.isGenerativeConstructor &&
5141-
backend.isNativeOrExtendsNative(constructor.enclosingClass) &&
5142-
!backend.isJsInterop(constructor)) {
5143-
// Native class generative constructors take a pre-constructed object.
5144-
inputs.add(graph.addConstantNull(compiler));
5140+
// Abstract class instantiation error takes precedence over argument
5141+
// mismatch.
5142+
ClassElement cls = constructor.enclosingClass;
5143+
if (cls.isAbstract && constructor.isGenerativeConstructor) {
5144+
generateAbstractClassInstantiationError(send, cls.name);
5145+
return;
51455146
}
5147+
51465148
// TODO(5347): Try to avoid the need for calling [implementation] before
51475149
// calling [makeStaticArgumentList].
51485150
constructorImplementation = constructor.implementation;
@@ -5152,6 +5154,14 @@ class SsaBuilder extends ast.Visitor
51525154
generateWrongArgumentCountError(send, constructor, send.arguments);
51535155
return;
51545156
}
5157+
5158+
var inputs = <HInstruction>[];
5159+
if (constructor.isGenerativeConstructor &&
5160+
backend.isNativeOrExtendsNative(constructor.enclosingClass) &&
5161+
!backend.isJsInterop(constructor)) {
5162+
// Native class generative constructors take a pre-constructed object.
5163+
inputs.add(graph.addConstantNull(compiler));
5164+
}
51555165
inputs.addAll(makeStaticArgumentList(callStructure,
51565166
send.arguments,
51575167
constructorImplementation));
@@ -5198,13 +5208,7 @@ class SsaBuilder extends ast.Visitor
51985208
} else {
51995209
SourceInformation sourceInformation =
52005210
sourceInformationBuilder.buildNew(send);
5201-
ClassElement cls = constructor.enclosingClass;
5202-
if (cls.isAbstract && constructor.isGenerativeConstructor) {
5203-
generateAbstractClassInstantiationError(send, cls.name);
5204-
return;
5205-
}
52065211
potentiallyAddTypeArguments(inputs, cls, expectedType);
5207-
52085212
addInlinedInstantiation(expectedType);
52095213
pushInvokeStatic(node, constructor, inputs,
52105214
typeMask: elementType,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2015, 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:expect/expect.dart";
6+
7+
// When an instantiation of an abstract class has the wrong arguments, an
8+
// AbstractClassInstantiationError is thrown, not a NoSuchMethodError.
9+
10+
abstract class A {
11+
A() { }
12+
}
13+
14+
class B {
15+
B() { }
16+
}
17+
18+
bool isAbstractClassInstantiationError(e) =>
19+
e is AbstractClassInstantiationError;
20+
21+
bool isNoSuchMethodError(e) => e is NoSuchMethodError;
22+
23+
void main() {
24+
Expect.throws(() => new A(), isAbstractClassInstantiationError);
25+
26+
Expect.throws(() => new B(1), isNoSuchMethodError);
27+
Expect.throws(() => new A(1), isAbstractClassInstantiationError);
28+
}

0 commit comments

Comments
 (0)