From 29ff25e70671ce2046aab5cf89bf519ec6cf2bed Mon Sep 17 00:00:00 2001 From: Iain Buclaw Date: Tue, 8 Sep 2020 17:15:33 +0200 Subject: [PATCH] d: Warn when casting from a D class to a C++ class. Before, the warning was only issued when casting in the other direction. Now a warning is printed for both directions. gcc/d/ChangeLog: * d-convert.cc (convert_expr): Warn when casting from a D class to a C++ class. gcc/testsuite/ChangeLog: * gdc.dg/Waddress.d: New test. * gdc.dg/Wcastresult1.d: New test. * gdc.dg/Wcastresult2.d: New test. --- gcc/d/d-convert.cc | 4 ++-- gcc/testsuite/gdc.dg/Waddress.d | 12 ++++++++++++ gcc/testsuite/gdc.dg/Wcastresult1.d | 18 ++++++++++++++++++ gcc/testsuite/gdc.dg/Wcastresult2.d | 12 ++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gdc.dg/Waddress.d create mode 100644 gcc/testsuite/gdc.dg/Wcastresult1.d create mode 100644 gcc/testsuite/gdc.dg/Wcastresult2.d diff --git a/gcc/d/d-convert.cc b/gcc/d/d-convert.cc index 5e3e855b2cb..40e84f05347 100644 --- a/gcc/d/d-convert.cc +++ b/gcc/d/d-convert.cc @@ -430,10 +430,10 @@ convert_expr (tree exp, Type *etype, Type *totype) /* d_convert will make a no-op cast. */ break; } - else if (cdfrom->isCPPclass ()) + else if (cdfrom->isCPPclass () || cdto->isCPPclass ()) { /* Downcasting in C++ is a no-op. */ - if (cdto->isCPPclass ()) + if (cdfrom->isCPPclass () && cdto->isCPPclass ()) break; /* Casting from a C++ interface to a class/non-C++ interface diff --git a/gcc/testsuite/gdc.dg/Waddress.d b/gcc/testsuite/gdc.dg/Waddress.d new file mode 100644 index 00000000000..74d7de80775 --- /dev/null +++ b/gcc/testsuite/gdc.dg/Waddress.d @@ -0,0 +1,12 @@ +// { dg-do compile } +// { dg-options "-Waddress" } + +void* ptr; + +int test() +{ + if (&ptr) // { dg-warning "the address of 'ptr' will always evaluate as 'true'" } + return 1; + + return 0; +} diff --git a/gcc/testsuite/gdc.dg/Wcastresult1.d b/gcc/testsuite/gdc.dg/Wcastresult1.d new file mode 100644 index 00000000000..759b338acdc --- /dev/null +++ b/gcc/testsuite/gdc.dg/Wcastresult1.d @@ -0,0 +1,18 @@ +// { dg-do compile } +// { dg-options "-Wcast-result" } + +extern(C++) class CPPClass +{ + int a; +} + +extern(D) class DClass +{ + int a; +} + +void test() +{ + auto cpptod = cast(DClass)new CPPClass; // { dg-warning "cast to 'Wcastresult1.DClass' will produce null result" } + auto dtocpp = cast(CPPClass)new DClass; // { dg-warning "cast to 'Wcastresult1.CPPClass' will produce null result" } +} diff --git a/gcc/testsuite/gdc.dg/Wcastresult2.d b/gcc/testsuite/gdc.dg/Wcastresult2.d new file mode 100644 index 00000000000..56d2dd20e82 --- /dev/null +++ b/gcc/testsuite/gdc.dg/Wcastresult2.d @@ -0,0 +1,12 @@ +// { dg-do compile } +// { dg-options "-Wcast-result" } + +void test() +{ + auto imvalue = 1.23i; + auto revalue = 1.23; + + auto imtore = cast(double)imvalue; // { dg-warning "cast from 'idouble' to 'double' will produce zero result" } + auto retoim = cast(idouble)revalue; // { dg-warning "cast from 'double' to 'idouble' will produce zero result" } + return; +} -- 2.30.2