/* * Copyright (c) 2023-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ function assert_ccexc(f: () => void) { try { f(); } catch (e) { assert(e instanceof ClassCastError); return; } assert false : "exception expected"; } class A { } class B { } class C { } class X { } function erase(x: Object | null | undefined): T { return x as T; } function test_substitution() { assert_ccexc(() => { erase(null); }) assert_ccexc(() => { erase(undefined); }) assert_ccexc(() => { erase(null); }) assert_ccexc(() => { erase(undefined); }) assert_ccexc(() => { erase(null); }) assert_ccexc(() => { erase(undefined); }) assert_ccexc(() => { erase(null); }) assert_ccexc(() => { erase(undefined); }) assert_ccexc(() => { erase(undefined); }) assert_ccexc(() => { erase(new Object()); }) assert_ccexc(() => { erase(new B()); }) assert_ccexc(() => { erase(undefined); }) assert_ccexc(() => { erase(new Object()); }) assert_ccexc(() => { erase(new C()); }) assert_ccexc(() => { erase(new B[0]); }) } class Erased { constructor(x: Object | null | undefined) { this.t = x as T; } t: T; } function test_substitution_memberexpr() { assert_ccexc(() => { new Erased(null).t; }) assert_ccexc(() => { new Erased(undefined).t; }) assert_ccexc(() => { new Erased(null).t; }) assert_ccexc(() => { new Erased(undefined).t; }) assert_ccexc(() => { new Erased(null).t; }) assert_ccexc(() => { new Erased(undefined).t; }) assert_ccexc(() => { new Erased(null).t; }) assert_ccexc(() => { new Erased(undefined).t; }) assert_ccexc(() => { new Erased(undefined).t; }) assert_ccexc(() => { new Erased(new Object()).t; }) assert_ccexc(() => { new Erased(new B()).t; }) assert_ccexc(() => { new Erased(undefined).t; }) assert_ccexc(() => { new Erased(new Object()).t; }) assert_ccexc(() => { new Erased(new C()).t; }) assert_ccexc(() => { new Erased(new B[0]).t; }) } function cast_to_tparam(x: Object | null | undefined) { x as T; } function test_constraint() { assert_ccexc(() => { cast_to_tparam(undefined); }) assert_ccexc(() => { cast_to_tparam(new Object()); }) assert_ccexc(() => { cast_to_tparam(new C()); }) } function to_basetype(x: Object | null | undefined) { return x as X; } function test_basetype() { assert_ccexc(() => { to_basetype(null); }) assert_ccexc(() => { to_basetype(undefined); }) assert_ccexc(() => { to_basetype(new Object()); }) assert_ccexc(() => { to_basetype(new C()); }) } function main() { test_substitution(); test_substitution_memberexpr(); test_constraint(); test_basetype(); }