• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16class A {
17  field: int = 5
18
19  constructor(arg: boolean) {
20  }
21
22  constructor(arg?: int) {
23    if (arg == undefined) {
24      this.field += 20
25    } else {
26      this.field += arg;
27      return; // Return statement is necessary to reproduce stack overflow and invalid constructor generation issue.
28    }
29  }
30}
31
32class B {
33  field: int = 5;
34
35  constructor(arg: boolean) {
36  }
37
38  constructor(arg: int) {
39    this.field += arg;
40    return; // Return statement is necessary to reproduce stack overflow issue.
41  }
42
43  constructor() {
44    this(15);
45    this.field += 30;
46  }
47}
48
49function main(): void {
50  assertEQ(new A(true).field, 5);  // field = 5
51  assertEQ(new A(10).field, 15);   // field = 5 + 10
52  assertEQ(new A().field, 25);     // field = 5 + 20
53
54  assertEQ(new B(true).field, 5);  // field = 5
55  assertEQ(new B(20).field, 25);   // field = 5 + 20
56  assertEQ(new B().field, 50);     // field = 5 + 15 + 30
57}
58