reject `f<typename T>' without crashing

Alexandre Oliva aoliva@redhat.com
Wed Dec 8 09:26:00 GMT 2004


This patch is for PR 18757.  The problem is two-fold.  First,
tsubst_template_args () returns error_mark_node if any of the substed
args is error_mark_node.  In the reported testcase, this turns a
vector containing an error_mark_node into an error_mark_node in
tsubst_baselink(), so we create a TEMPLATE_ID_EXPR with that as the
explicit template argument list.

Later on, in build_new_method_call(), when we call
add_template_candidate(), we end up getting to coerce_template_args()
with args == error_mark_node, at which point we ICE if checking is
enabled, or dereference an invalid pointer otherwise, as described in
the bug report.

The following patch seems to be enough to fix the problem.  It ensures
that we don't silently accept a template id with parse errors in its
explicit arguments as if it had parsed successfully (otherwise we end
up silently compiling code with syntax errors, if a template
containing the ill-formed template id turns out to never be
instantiated).

It might also be reasonable to modify coerce_template_args() to test
for an error_mark_node in args and return an error_mark_node
immediately, but I couldn't convince myself this wouldn't fail to
issue error messages in case of errors in template argument
substitution.

I've also attached the testcase I intend to install in the testsuite,
after annotating it properly.  I'm not entirely happy with the
multiple errors I get for each syntax error, but I don't see how to do
any better, since we end up attempting to parse the ill-formed
template argument as a type cast.

BZ141300s3.cc: In member function ‘void A::bar(T)’:
BZ141300s3.cc:4: error: expected nested-name-specifier before ‘T’
BZ141300s3.cc:4: error: expected `(' before ‘>’ token
BZ141300s3.cc: In member function ‘void A::bad(T)’:
BZ141300s3.cc:5: error: expected nested-name-specifier before ‘T’
BZ141300s3.cc:5: error: expected `(' before ‘>’ token
BZ141300s3.cc: In member function ‘void B<T>::bar(T)’:
BZ141300s3.cc:11: error: expected nested-name-specifier before ‘T’
BZ141300s3.cc:11: error: expected `(' before ‘>’ token
BZ141300s3.cc: In member function ‘void B<T>::bad(T)’:
BZ141300s3.cc:12: error: template argument 1 is invalid
BZ141300s3.cc:12: error: template argument 1 is invalid
BZ141300s3.cc:12: error: ‘B<T>’ is not a template

Comments?  Ok to install if it passes regression testing, currently
ongoing on x86_64-linux-gnu?  

-------------- next part --------------
A non-text attachment was scrubbed...
Name: cp-reject-typename-template-arg.patch
Type: text/x-patch
Size: 1041 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20041208/0f9a0c33/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: BZ141300s3.cc
Type: text/x-c++
Size: 373 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20041208/0f9a0c33/attachment-0001.bin>
-------------- next part --------------

-- 
Alexandre Oliva             http://www.ic.unicamp.br/~oliva/
Red Hat Compiler Engineer   aoliva@{redhat.com, gcc.gnu.org}
Free Software Evangelist  oliva@{lsd.ic.unicamp.br, gnu.org}


More information about the Gcc-patches mailing list