Bug 36607 - [4.3 Regression] Incorrect type diagnostic on substracting casted char pointers
Summary: [4.3 Regression] Incorrect type diagnostic on substracting casted char pointers
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.4.0
: P2 normal
Target Milestone: 4.3.4
Assignee: Richard Biener
URL: http://gcc.gnu.org/ml/gcc-patches/200...
Keywords: patch, rejects-valid
Depends on:
Blocks:
 
Reported: 2008-06-23 16:08 UTC by Robert Schiele
Modified: 2009-06-19 21:46 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.0.1 4.1.1 4.3.4 4.4.0
Known to fail: 4.3.3
Last reconfirmed: 2008-12-30 06:14:14


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Robert Schiele 2008-06-23 16:08:22 UTC
The following little code sample does no longer compile with 4.3 or higher:

struct a {};
void b() {
    int a::*m;
    a *c;
    int p = reinterpret_cast<char*>(&(c->*m)) - reinterpret_cast<char*>(c);
}

Instead the compiler emits the error message:

bad.cc: In function ‘void b()’:
bad.cc:5: error: aggregate value used where an integer was expected

Though it is clear that this code sample is not perfectly valid according to the C++ standard since "reinterpret_cast<char*>(&(c->*m))" and "reinterpret_cast<char*>(c)" are not part of the same array I would not expect this kind of behavior since the claim of the compiler that there is something wrong with types is completely bogus.

Rewriting the code like the following makes the compiler accepting it again:

struct a {};
void b() {
    int a::*m;
    a *c;
    char* d = reinterpret_cast<char*>(&(c->*m));
    char* e = reinterpret_cast<char*>(c);
    int p = d - e;
}
Comment 1 Andrew Pinski 2008-12-28 00:40:24 UTC
This works for me with 4.3.2 and the current trunk.

Does it work for you?
Comment 2 Robert Schiele 2008-12-30 06:07:13 UTC
It works for you? This is weired!

Just tried it with current trunk again and it does still show exactly the same error there.
Comment 3 Andrew Pinski 2008-12-30 06:14:14 UTC
Ok, it fails with -m64, looking into it.

Confirmed.
Comment 4 Andrew Pinski 2008-12-30 16:57:30 UTC
Some folding optimizing is changing "reinterpret_cast<char*>(&(c->*m)) - reinterpret_cast<char*>(c)" into (long int)(m) and then we have an implicit conversion into int:
	  return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));

Comment 5 Andrew Pinski 2008-12-30 17:13:19 UTC
I have a simple fix, the front-end should have already warned about the conversion from a pointer to a member object to an integer type.  Therefor convert can handle OFFSET_TYPE just like INTEGER_TYPE and such.
Comment 6 Richard Biener 2009-01-24 10:20:32 UTC
GCC 4.3.3 is being released, adjusting target milestone.
Comment 7 Paolo Bonzini 2009-02-03 13:56:45 UTC
ping?
Comment 8 pinskia@gmail.com 2009-02-03 15:44:29 UTC
Subject: Re:  [4.3/4.4 Regression] Incorrect type diagnostic on substracting casted char pointers



Sent from my iPhone

On Feb 3, 2009, at 5:56 AM, "bonzini at gnu dot org" <gcc-bugzilla@gcc.gnu.org 
 > wrote:

>
>
> ------- Comment #7 from bonzini at gnu dot org  2009-02-03 13:56  
> -------
> ping?

The patch was just approved last night and I will be applying it when  
I get into work today.

>
>
>
> -- 
>
> bonzini at gnu dot org changed:
>
>           What    |Removed                     |Added
> --- 
> --- 
> ----------------------------------------------------------------------
>                 CC|                            |bonzini at gnu dot org
>
>
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36607
>
Comment 9 Andrew Pinski 2009-02-03 22:38:27 UTC
Subject: Bug 36607

Author: pinskia
Date: Tue Feb  3 22:38:16 2009
New Revision: 143909

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=143909
Log:
2009-02-03  Andrew Pinski  <andrew_pinski@playstation.sony.com>

        PR C++/36607
        * convert.c (convert_to_integer): Treat OFFSET_TYPE like INTEGER_TYPE.

2009-02-03  Andrew Pinski  <andrew_pinski@playstation.sony.com>

        PR C++/36607
        * g++.dg/expr/cast10.C: New test.



Added:
    trunk/gcc/testsuite/g++.dg/expr/cast10.C
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/convert.c
    trunk/gcc/testsuite/ChangeLog

Comment 10 Andrew Pinski 2009-02-03 22:39:05 UTC
Fixed on the trunk at least.
Comment 11 Andrew Pinski 2009-04-16 15:43:52 UTC
I am no longer working on this one.
Comment 12 Richard Biener 2009-06-19 16:51:07 UTC
Testing a backport.
Comment 13 Richard Biener 2009-06-19 21:44:41 UTC
Subject: Bug 36607

Author: rguenth
Date: Fri Jun 19 21:44:24 2009
New Revision: 148730

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=148730
Log:
2009-06-19  Richard Guenther  <rguenther@suse.de>

	Backport from mainline:
	2009-02-03  Andrew Pinski  <andrew_pinski@playstation.sony.com>

	PR C++/36607
	* convert.c (convert_to_integer): Treat OFFSET_TYPE like INTEGER_TYPE.

	* g++.dg/expr/cast10.C: New test.

	2009-02-03  Jakub Jelinek  <jakub@redhat.com>
 
	PR target/35318
	* function.c (match_asm_constraints_1): Skip over
	initial optional % in the constraint.
 
	* gcc.c-torture/compile/pr35318.c: New test.

	2009-05-20  Jakub Jelinek  <jakub@redhat.com>

	PR middle-end/40204
	* fold-const.c (fold_binary) <case BIT_AND_EXPR>: Avoid infinite
	recursion if build_int_cst_type returns the same INTEGER_CST as
	arg1.

	* gcc.c-torture/compile/pr40204.c: New test.

Added:
    branches/gcc-4_3-branch/gcc/testsuite/g++.dg/expr/cast10.C
    branches/gcc-4_3-branch/gcc/testsuite/gcc.c-torture/compile/pr35318.c
    branches/gcc-4_3-branch/gcc/testsuite/gcc.c-torture/compile/pr40204.c
Modified:
    branches/gcc-4_3-branch/gcc/ChangeLog
    branches/gcc-4_3-branch/gcc/convert.c
    branches/gcc-4_3-branch/gcc/fold-const.c
    branches/gcc-4_3-branch/gcc/function.c
    branches/gcc-4_3-branch/gcc/testsuite/ChangeLog

Comment 14 Richard Biener 2009-06-19 21:46:23 UTC
Fixed.