|
| 1 | +// RUN: split-file %s %t |
| 2 | +// RUN: %target-swift-frontend -typecheck -verify -suppress-remarks -suppress-notes \ |
| 3 | +// RUN: -cxx-interoperability-mode=default \ |
| 4 | +// RUN: -I %t/Inputs %t/main.swift \ |
| 5 | +// RUN: -verify-additional-file %t%{fs-sep}Inputs%{fs-sep}header.h |
| 6 | + |
| 7 | +//--- Inputs/module.modulemap |
| 8 | +module CxxHeader { |
| 9 | + header "header.h" |
| 10 | + requires cplusplus |
| 11 | +} |
| 12 | + |
| 13 | +//--- Inputs/header.h |
| 14 | +#pragma once |
| 15 | + |
| 16 | +// TODO: the following diagnostics should be moved to main.swift at the call site |
| 17 | +// that triggers the instantiation |
| 18 | +// expected-error@+4 {{could not substitute parameters for C++ function template 'cxxCast': BaseT, UnrelatedT}} |
| 19 | +// expected-error@+3 {{could not substitute parameters for C++ function template 'cxxCast': BaseT, SubClassT}} |
| 20 | +// expected-error@+2 {{could not substitute parameters for C++ function template 'cxxCast': BaseT, SubProtectedT}} |
| 21 | +// expected-error@+1 {{could not substitute parameters for C++ function template 'cxxCast': BaseT, SubPrivateT}} |
| 22 | +template <class O, class I> O cxxCast(I i) { return static_cast<O>(i); } |
| 23 | +// expected-error@-1 {{cannot cast 'const SubPrivateT' to its private base class 'const BaseT'}} |
| 24 | +// expected-error@-2 {{cannot cast 'const SubProtectedT' to its protected base class 'const BaseT'}} |
| 25 | +// expected-error@-3 {{cannot cast 'const SubClassT' to its private base class 'const BaseT'}} |
| 26 | +// expected-error@-4 {{no matching conversion for static_cast from 'UnrelatedT' to 'BaseT'}} |
| 27 | + |
| 28 | +struct BaseT { }; |
| 29 | + |
| 30 | +struct SubT : BaseT { }; // publicly inherit from BaseT |
| 31 | +struct SubPrivateT : private BaseT { }; // privately inherit from BaseT |
| 32 | +struct SubProtectedT : protected BaseT { }; // privately inherit from BaseT |
| 33 | +class SubClassT : BaseT { }; // privately inherit from BaseT |
| 34 | +struct UnrelatedT { }; // does not inherit from BaseT |
| 35 | + |
| 36 | + |
| 37 | +//--- main.swift |
| 38 | +import CxxHeader |
| 39 | + |
| 40 | +let _: BaseT = cxxCast(SubT()) // valid: upcast to public base |
| 41 | +let _: BaseT = cxxCast(SubPrivateT()) // invalid: upcast to non-public base |
| 42 | +let _: BaseT = cxxCast(SubProtectedT()) // invalid: upcast to non-public base |
| 43 | +let _: BaseT = cxxCast(SubClassT()) // invalid: upcast to non-public base |
| 44 | +let _: BaseT = cxxCast(UnrelatedT()) // invalid: cast to unrelated type |
0 commit comments