Bug 48396 - std::type_info is implicitly declared
Summary: std::type_info is implicitly declared
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.7.0
: P3 normal
Target Milestone: 12.0
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid, rejects-valid
Depends on:
Blocks:
 
Reported: 2011-03-31 22:14 UTC by Paolo Carlini
Modified: 2021-09-15 11:21 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-09-14 00:00:00


Attachments
patch to allow redeclaration in module (453 bytes, patch)
2021-09-14 20:49 UTC, Jason Merrill
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Paolo Carlini 2011-03-31 22:14:24 UTC
This sort of code - note, no includes - is wrongly accepted:

namespace std
{
  type_info* fun1();
  void fun2(type_info);
}
Comment 1 Jonathan Wakely 2021-03-05 10:09:55 UTC
Adjusting the summary because this is specific to std::type_info, it doesn't happen for other types.

I think there's some historical reason that G++ pre-defines std::type_info, but maybe we could make that only valid in system headers. It still causes bugs even then, because libstdc++ can accidentally rely on the G++ behaviour, which doesn't work with other compilers (e.g PR 56468).
Comment 2 Jonathan Wakely 2021-09-14 20:00:03 UTC
This breaks modules, and makes it very difficult to export a 'std' module, as proposed by https://wg21.link/p2412r0

export module std;

// define it
namespace std
{
  class type_info;
}

// exports
export namespace std { };

namespace std
{
  export class type_info;
}

g++ -fmodules-ts -fmodule-only std.cc

std.cc:6:9: error: cannot declare 'struct std::type_info' in a different module
    6 |   class type_info;
      |         ^~~~~~~~~
<built-in>: note: declared here
std.cc:14:16: error: cannot declare 'struct std::type_info' in a different module
   14 |   export class type_info;
      |                ^~~~~~~~~
<built-in>: note: declared here
std.cc:1:8: warning: not writing module 'std' due to errors
    1 | export module std;
      |        ^~~~~~


As a stop-gap, could we maybe suppress the implicit definition when -fmodules-ts is in use? Otherwise I am unable to prototype the proposal.
Comment 3 Jonathan Wakely 2021-09-14 20:05:09 UTC
cp/rtti.c defines:

/* Declare language defined type_info type and a pointer to const
   type_info.  This is incomplete here, and will be completed when
   the user #includes <typeinfo>.  There are language defined
   restrictions on what can be done until that is included.  Create
   the internal versions of the ABI types.  */

void
init_rtti_processing (void)
{
  push_nested_namespace (std_node);
  tree type_info_type = xref_tag (class_type, get_identifier ("type_info"));
  pop_nested_namespace (std_node);
  const_type_info_type_node
    = cp_build_qualified_type (type_info_type, TYPE_QUAL_CONST);
  type_info_ptr_type = build_pointer_type (const_type_info_type_node);

  vec_alloc (unemitted_tinfo_decls, 124);

  create_tinfo_types ();
}

Is there a way to do that without making it visible to user code? Or without implicitly adding it to the GMF?
Comment 4 Jason Merrill 2021-09-14 20:49:17 UTC
Created attachment 51462 [details]
patch to allow redeclaration in module

This isn't a proper fix, but may let you make progress with modularization.
Comment 5 GCC Commits 2021-09-15 04:27:45 UTC
The master branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:a53781c8fd258608780821168a7f5faf7be63690

commit r12-3538-ga53781c8fd258608780821168a7f5faf7be63690
Author: Jason Merrill <jason@redhat.com>
Date:   Tue Sep 14 17:37:27 2021 -0400

    c++: don't predeclare std::type_info [PR48396]
    
    We've always predeclared std::type_info, which has been wrong for a while,
    but now with modules it becomes more of a practical problem, if we want to
    declare it in the purview of a module.  So don't predeclare it.  For
    building up the type_info information to write out with the vtable, we can
    use void* instead of type_info*, since they already aren't the real types.
    
            PR c++/48396
    
    gcc/cp/ChangeLog:
    
            * cp-tree.h (enum cp_tree_index): Remove CPTI_TYPE_INFO_PTR_TYPE.
            (type_info_ptr_type): Remove.
            * rtti.c (init_rtti_processing): Don't predeclare std::type_info.
            (typeid_ok_p): Check for null const_type_info_type_node.
            (type_info_ptr_type, get_void_tinfo_ptr): New fns.
            (get_tinfo_decl_dynamic, get_tinfo_ptr): Use them.
            (ptr_initializer, ptm_initializer, get_pseudo_ti_init): Use them.
            (get_tinfo_desc): Use const_ptr_type_node.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/rtti/undeclared1.C: New test.
Comment 6 Jason Merrill 2021-09-15 04:28:19 UTC
Fixed.