Bug 50958 - [C++0x] raw literal operator provides incorrect string for integer literal '0'
Summary: [C++0x] raw literal operator provides incorrect string for integer literal '0'
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.7.0
: P3 normal
Target Milestone: 4.7.0
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2011-11-02 09:16 UTC by Daniel Krügler
Modified: 2011-11-21 19:48 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Patch to correctly resolve literal operators. (3.30 KB, application/octet-stream)
2011-11-16 16:03 UTC, Ed Smith-Rowland
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Daniel Krügler 2011-11-02 09:16:10 UTC
gcc 4.7.0 20111029 (experimental) in C++0x mode rejects the following code:

//---
typedef decltype(sizeof(0)) size_type;

constexpr size_type cstrlen_impl(const char* s, size_type i)
{
  return s[i] ? cstrlen_impl(s, i + 1) : i;
}

constexpr size_type cstrlen(const char* s)
{
  return s ? cstrlen_impl(s, 0) : throw 0;
}

constexpr size_type operator "" _lenraw(const char* digits)
{
  return cstrlen(digits);
}

static_assert(1_lenraw == 1, "Ouch"); // OK
static_assert(0_lenraw == 1, "Ouch"); // Error
//---

The error I'm getting is:

main.cpp|19|error: non-constant condition for static assertion|
main.cpp|19|  in constexpr expansion of 'operator"" _lenraw(0u)'|
main.cpp|15|  in constexpr expansion of 'cstrlen(digits)'|
main.cpp|10|error: expression '<throw-expression>' is not a constant-expression|

This allows the conclusion, that the octal integer literal '0' is incorrectly handled, instead of the expected string length of 1 the raw literal operator gets a NULL string pointer. This seems to violate 2.14.8 p3 where 'n' would be equal to '0' which should lead to the effective string literal argument "0" in this case.
Comment 1 Ed Smith-Rowland 2011-11-02 14:00:11 UTC
In http://gcc.gnu.org/ml/gcc-patches/2011-10/msg02804.html
I have a patch to address a resolution issue.

For some reason, this bug doesn't show up in that version.

I'll try to understand this bug but ultimately (and soon) I'd like to put a version of that patch in and call it done.
Comment 2 Daniel Krügler 2011-11-09 10:42:18 UTC
(In reply to comment #1)
I have now tested gcc 4.7.0 20111105, but the problem still seems to exist. I can reproduce it on Windows XP 32 bit and Windows 7 64 bit.
Comment 3 Ed Smith-Rowland 2011-11-16 16:03:26 UTC
Created attachment 25839 [details]
Patch to correctly resolve literal operators.

Literal operators were not properly resolved according to .  This means that there could be mismatches between the literal and the literal operator that is used to resolve it.

This patch also fixes this bug.
Comment 4 Jason Merrill 2011-11-21 19:27:35 UTC
Author: jason
Date: Mon Nov 21 19:27:30 2011
New Revision: 181595

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=181595
Log:
	PR c++/50958
gcc/cp/
	* parser.c (lookup_literal_operator): New.
	(cp_parser_userdef_char_literal): Use it.
	(cp_parser_userdef_numeric_literal): Use it.
	(cp_parser_userdef_string_literal): Use lookup_name.
libcpp/
	* expr.c (cpp_userdef_char_remove_type): Fix typo.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/udlit-implicit-conv-neg.C
    trunk/gcc/testsuite/g++.dg/cpp0x/udlit-raw-length.C
    trunk/gcc/testsuite/g++.dg/cpp0x/udlit-resolve.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/parser.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/c-c++-common/dfp/pr33466.c
    trunk/gcc/testsuite/g++.dg/cpp0x/udlit-declare-neg.C
    trunk/gcc/testsuite/g++.dg/cpp0x/udlit-member-neg.C
    trunk/gcc/testsuite/g++.dg/cpp0x/udlit-raw-op-string-neg.C
    trunk/libcpp/ChangeLog
    trunk/libcpp/expr.c
Comment 5 Jason Merrill 2011-11-21 19:48:07 UTC
Fixed.