This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Turning warning "returning reference to temporary" into error?
On 11-May-2003, Zack Weinberg <zack@codesourcery.com> wrote:
> Gabriel Dos Reis <gdr@integrable-solutions.net> writes:
>
> > A compiler writer cannot imagine every possible usage. Which is why
> > somethings are warnings. In this case, if the return value is ignored
> > then the program is valid. GCC cannot reject it just because
> > -pedantic is specified.
>
> In other places where we can prove that execution of a certain code
> path will cause undefined behavior, but we can't prove that that path
> will be taken at runtime, we issue a warning and generate a trap
> instruction. An analogous tactic for this scenario would be if we
> generated code to return a distinguished pointer value that's
> guaranteed to cause a segmentation fault when dereferenced. (This
> should _not_ be 0, as that may mask the bug.)
I don't think that would be a good idea.
Although the behaviour is undefined, there are some important programs
that rely on the assumption that the address of a local is an address
on the stack. In particular, the Boehm et al. conservative garbage
collector does this. If I recall correctly, some versions of
LinuxThreads also did this (to implement pthread_self()),
although I don't know off-hand if they still use that technique.
If GCC was changed in a way that stopped this technique from working,
conservative garbage collectors would have to resort to techniques
which are *less* portable, such as inline assembler. I don't think
that would be an improvement, and I definitely don't think it would
be worth the transition cost.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
The University of Melbourne | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.