Bug 87547 (bit-field-type-names) - G++ reports bad type names for bit-field members
Summary: G++ reports bad type names for bit-field members
Status: RESOLVED FIXED
Alias: bit-field-type-names
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: unknown
: P3 normal
Target Milestone: 9.0
Assignee: Jakub Jelinek
URL:
Keywords: ABI, wrong-code
Depends on:
Blocks:
 
Reported: 2018-10-08 05:32 UTC by Sandro Boehler
Modified: 2021-07-31 23:16 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-10-08 00:00:00


Attachments
gcc9-pr87547.patch (609 bytes, patch)
2018-10-09 10:12 UTC, Jakub Jelinek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Sandro Boehler 2018-10-08 05:32:10 UTC
Consider the following code:

#include <typeinfo>
struct S
{
    unsigned int n4  :  4;
    unsigned int n12 : 12;
};

void f(unsigned char)
{
    std::cout << "uc" << std::endl;
}

void f(unsigned short)
{
    std::cout << "us" << std::endl;
}

void f(unsigned int)
{
    std::cout << "ui" << std::endl;
}

int main(int argc, char* argv[])
{
    S s; s.n4 = 0; s.n12 = 0;
    f(s.n4);
    f(s.n12);
    std::cout << typeid(s.n4).name() << std::endl;
    std::cout << typeid(s.n12).name() << std::endl;
    std::cout << typeid(unsigned char).name() << std::endl;
    std::cout << typeid(unsigned short).name() << std::endl;
    std::cout << typeid(unsigned int).name() << std::endl;
    return 0;
}

Expected output (according to standard [class.bit] (http://eel.is/c++draft/class.bit): "The bit-field attribute is not part of the type of the class member."):

ui
ui
j
j
h
t
j

Output produced with g++ (Ubuntu 8.1.0-5ubuntu1~16.04) 8.1.0 (initially discovered with 5.5.0):

ui
ui
h
t
h
t
j

So while the overload is selected correctly, typeid selects the bad type.

I did not test separately, but assume the compiler showing same behaviour for C as well.
Comment 1 Jonathan Wakely 2018-10-08 10:44:05 UTC
(In reply to Sandro Boehler from comment #0)
> Expected output (according to standard [class.bit]
> (http://eel.is/c++draft/class.bit): "The bit-field attribute is not part of
> the type of the class member."):

If the bit-field attribute was part of the type then it would be "unsigned int:4" not "unsigned char" or "unsigned short". But G++ is still wrong to use a smaller type, it should be unsigned int (even if internally something else is used).
Comment 2 Jakub Jelinek 2018-10-09 10:12:07 UTC
Created attachment 44816 [details]
gcc9-pr87547.patch

Untested fix.
Comment 3 Jakub Jelinek 2018-10-11 07:07:54 UTC
Author: jakub
Date: Thu Oct 11 07:07:22 2018
New Revision: 265033

URL: https://gcc.gnu.org/viewcvs?rev=265033&root=gcc&view=rev
Log:
	PR c++/87547
	* rtti.c (get_tinfo_decl_dynamic): Use unlowered_expr_type instead
	of TREE_TYPE.

	* g++.dg/rtti/typeid12.C: New test.

Added:
    trunk/gcc/testsuite/g++.dg/rtti/typeid12.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/rtti.c
    trunk/gcc/testsuite/ChangeLog
Comment 4 Andrew Pinski 2021-07-31 23:16:27 UTC
Fixed so closing as such.