Skip to content

Equal typeid for nonequal types in anonymous namespaces #59790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Alcaro opened this issue Jan 2, 2023 · 1 comment
Open

Equal typeid for nonequal types in anonymous namespaces #59790

Alcaro opened this issue Jan 2, 2023 · 1 comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema"

Comments

@Alcaro
Copy link
Contributor

Alcaro commented Jan 2, 2023

#include <typeinfo>

namespace {
	struct my_x {
		static int member;
	};
	int my_x::member;
}

int* get_member() { return &my_x::member; }
const std::type_info* inf() { return &typeid(my_x); }
#include <typeinfo>

namespace {
	struct my_x {
		static int member;
	};
	int my_x::member;
}

int* get_member();
const std::type_info* inf();

int* get_member2() { return &my_x::member; }
const std::type_info* inf2() { return &typeid(my_x); }

#include <stdio.h>

int main()
{
	printf("%p %p\n", get_member(), get_member2());
	printf("%p %p\n", inf(), inf2());
	printf("%d\n", get_member() == get_member2());
	printf("%d\n", *inf() == *inf2());
}

https://godbolt.org/z/Pjnz6jbGb

This prints four pointers, then 0 1.

I believe that is an illegal output. Either the two my_x are the same, and it should print 1 1, or they're different, and it should print 0 0. I think 0 0 is the correct answer, but I'm not very familiar with this corner of the C++ specification.

Similar to #10492 - that one's long gone, but maybe a similar fix would apply here. (GCC has a similar bug, but only with -fmerge-all-constants. edit: And that one's documented as giving noncompliant behavior, so I'm not sure if it is a bug.)

@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" and removed new issue labels Jan 2, 2023
@llvmbot
Copy link
Member

llvmbot commented Jan 2, 2023

@llvm/issue-subscribers-clang-frontend

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema"
Projects
None yet
Development

No branches or pull requests

3 participants