llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.78k stars 11.9k forks source link

compiling w `-fno-exceptions` generates errors on `throw` in templates that are never instantiated #26590

Open mclow opened 8 years ago

mclow commented 8 years ago
Bugzilla Link 26216
Version trunk
OS All
CC @DougGregor,@DimitryAndric,@zygoloid

Extended Description

I ran into this in the libc++ test suite.

If -fno-exceptions is defined, the following code compiles w/o error using GCC, but gets an error with clang.

My expectation matches gcc's behavior; no error should be generated unless/until the template is instantiated.

Reproducer:

#include <iostream>
#include <stdexcept>

template <typename T>
struct wrap {
    wrap() : t_() {}
    wrap(const T& t) : t_(t) {}

    T & get() { return t_; }
    void toss () { throw std::runtime_error("Toss"); }
    T t_;
};

int main() {
    std::cout << "Hello World" << std::endl;
    }
llvmbot commented 5 years ago

I get this issue with a function template which is not being invoked or instantiated either implicitly or explicitly even with -fdelayed-template-parsing. This is the only part of my source code which seems to cause this issue, I use the technique of separating exception generating code ('Safe' versions) throughout the library yet only this header seems to cause the issue for some unknown (to me) reason.

While compiling this file:

https://github.com/ASA1976/RAP-BTL/blob/20575d451cef6c66a72384d1b49f09fe1441cadd/invocation.cpp

This header causes the error:

https://github.com/ASA1976/RAP-BTL/blob/20575d451cef6c66a72384d1b49f09fe1441cadd/abstraction.hpp

And this header is also required to compile that particular source file:

https://github.com/ASA1976/RAP-BTL/blob/20575d451cef6c66a72384d1b49f09fe1441cadd/location.hpp

mclow commented 8 years ago

or what the standard(s) say about it, though.

I'm pretty sure that the standard says nothing about compiling with exceptions off.

DimitryAndric commented 8 years ago

If you use -fdelayed-template-parsing, this example will work. I do not know whether this is intentional or not, or what the standard(s) say about it, though. :-)