rust/compiler
bors d3a8524e80 Auto merge of #129137 - camelid:lazy-def-macro-const, r=BoxyUwU
Fix anon const def-creation when macros are involved

Fixes #128016.

Ever since #125915, some `ast::AnonConst`s turn into `hir::ConstArgKind::Path`s,
which don't have associated `DefId`s. To deal with the fact that we don't have
resolution information in `DefCollector`, we decided to implement a process
where if the anon const *appeared* to be trivial (i.e., `N` or `{ N }`), we
would avoid creating a def for it in `DefCollector`. If later, in AST lowering,
we realized it turned out to be a unit struct literal, or we were lowering it
to something that didn't use `hir::ConstArg`, we'd create its def there.

However, let's say we have a macro `m!()` that expands to a reference to a free
constant `FOO`. If we use `m!()` in the body of an anon const (e.g., `Foo<{ m!() }>`),
then in def collection, it appears to be a nontrivial anon const and we create
a def. But the macro expands to something that looks like a trivial const arg,
but is not, so in AST lowering we "fix" the mistake we assumed def collection
made and create a def for it. This causes a duplicate definition ICE.

The long-term fix for this is to delay the creation of defs for all expression-like
nodes until AST lowering (see #128844 for an incomplete attempt at this). This
would avoid issues like this one that are caused by hacky workarounds. However,
doing this uncovers a pre-existing bug with opaque types that is quite involved
to fix (see #129023).

In the meantime, this PR fixes the bug by delaying def creation for anon consts
whose bodies are macro invocations until after we expand the macro and know
what is inside it. This is accomplished by adding information to create the
anon const's def to the data in `Resolver.invocation_parents`.

r? `@BoxyUwU`
2024-09-13 01:10:51 +00:00
..
rustc disable size asserts in the compiler when randomizing layouts 2024-08-31 23:56:45 +02:00
rustc_abi inhibit layout randomization for Box 2024-08-31 23:56:45 +02:00
rustc_arena compiler: Remove feature(new_uninit) 2024-08-27 10:17:05 -07:00
rustc_ast Fix anon const def-creation when macros are involved 2024-09-12 13:48:30 -04:00
rustc_ast_ir Add warn(unreachable_pub) to several crates. 2024-08-16 08:46:13 +10:00
rustc_ast_lowering Re-enable ConstArgKind::Path lowering by default 2024-09-12 13:56:01 -04:00
rustc_ast_passes Rollup merge of #130252 - compiler-errors:const-gen, r=chenyukang 2024-09-12 20:37:18 +10:00
rustc_ast_pretty Add initial support for raw lifetimes 2024-09-06 10:32:48 -04:00
rustc_attr Remove needless returns detected by clippy in the compiler 2024-09-09 13:32:22 +02:00
rustc_baked_icu_data Don't add warn(unreachable_pub) to rustc_baked_icu. 2024-08-16 08:46:52 +10:00
rustc_borrowck Rollup merge of #130235 - compiler-errors:nested-if, r=michaelwoerister 2024-09-12 20:37:16 +10:00
rustc_builtin_macros Rollup merge of #130235 - compiler-errors:nested-if, r=michaelwoerister 2024-09-12 20:37:16 +10:00
rustc_codegen_cranelift Fix the examples in cg_clif 2024-09-09 19:39:43 -07:00
rustc_codegen_gcc Rollup merge of #129981 - nnethercote:rm-serialize_bitcode, r=antoyo,tmiasko 2024-09-09 19:20:36 -07:00
rustc_codegen_llvm Rollup merge of #130235 - compiler-errors:nested-if, r=michaelwoerister 2024-09-12 20:37:16 +10:00
rustc_codegen_ssa Rollup merge of #130235 - compiler-errors:nested-if, r=michaelwoerister 2024-09-12 20:37:16 +10:00
rustc_const_eval Rollup merge of #130250 - compiler-errors:useless-conversion, r=jieyouxu 2024-09-12 20:37:17 +10:00
rustc_data_structures Add an internal lint that warns when accessing untracked data 2024-09-03 19:14:19 +02:00
rustc_driver
rustc_driver_impl Simplify some nested if statements 2024-09-11 13:45:23 -04:00
rustc_error_codes Ban non-array SIMD 2024-09-09 19:39:43 -07:00
rustc_error_messages Add warn(unreachable_pub) to rustc_error_messages. 2024-08-27 11:52:08 +10:00
rustc_errors clippy::useless_conversion 2024-09-11 17:52:53 -04:00
rustc_expand Remove needless returns detected by clippy in the compiler 2024-09-09 13:32:22 +02:00
rustc_feature Re-enable ConstArgKind::Path lowering by default 2024-09-12 13:56:01 -04:00
rustc_fluent_macro Add warn(unreachable_pub) to rustc_fluent_macro. 2024-08-27 12:56:54 +10:00
rustc_fs_util
rustc_graphviz Add warn(unreachable_pub) to rustc_graphviz. 2024-08-27 12:58:29 +10:00
rustc_hir disallow naked_asm! outside of #[naked] functions 2024-09-10 15:19:14 +02:00
rustc_hir_analysis Rollup merge of #130250 - compiler-errors:useless-conversion, r=jieyouxu 2024-09-12 20:37:17 +10:00
rustc_hir_pretty Add warn(unreachable_pub) to rustc_hir_pretty. 2024-08-27 13:25:40 +10:00
rustc_hir_typeck Rollup merge of #130250 - compiler-errors:useless-conversion, r=jieyouxu 2024-09-12 20:37:17 +10:00
rustc_incremental Also fix if in else 2024-09-11 17:24:01 -04:00
rustc_index IntervalSet: add comment about representation 2024-09-08 16:54:28 +02:00
rustc_index_macros
rustc_infer Rollup merge of #130114 - eduardosm:needless-returns, r=compiler-errors 2024-09-11 15:53:22 -07:00
rustc_interface Auto merge of #130249 - compiler-errors:sad-new-solver-coherence, r=lcnr 2024-09-12 10:17:32 +00:00
rustc_lexer Add initial support for raw lifetimes 2024-09-06 10:32:48 -04:00
rustc_lint Rollup merge of #130235 - compiler-errors:nested-if, r=michaelwoerister 2024-09-12 20:37:16 +10:00
rustc_lint_defs Rollup merge of #126452 - compiler-errors:raw-lifetimes, r=spastorino 2024-09-07 23:30:10 +02:00
rustc_llvm update cfgs 2024-09-05 17:24:01 +01:00
rustc_log
rustc_macros Add warn(unreachable_pub) to rustc_metadata. 2024-08-29 20:13:06 +10:00
rustc_metadata Auto merge of #130036 - weiznich:diagnostic_unstable_tracking, r=compiler-errors 2024-09-08 23:39:00 +00:00
rustc_middle Re-enable ConstArgKind::Path lowering by default 2024-09-12 13:56:01 -04:00
rustc_mir_build Rollup merge of #130235 - compiler-errors:nested-if, r=michaelwoerister 2024-09-12 20:37:16 +10:00
rustc_mir_dataflow Remove Gatherer. 2024-09-09 16:33:27 +10:00
rustc_mir_transform Rollup merge of #130263 - Zalathar:sums, r=compiler-errors 2024-09-12 19:03:42 +02:00
rustc_monomorphize Rollup merge of #130235 - compiler-errors:nested-if, r=michaelwoerister 2024-09-12 20:37:16 +10:00
rustc_next_trait_solver Rollup merge of #130273 - lcnr:overflow-no-constraints, r=compiler-errors 2024-09-12 19:03:43 +02:00
rustc_parse Rollup merge of #130250 - compiler-errors:useless-conversion, r=jieyouxu 2024-09-12 20:37:17 +10:00
rustc_parse_format Add warn(unreachable_pub) to rustc_parse_format. 2024-08-29 20:13:06 +10:00
rustc_passes Rollup merge of #130235 - compiler-errors:nested-if, r=michaelwoerister 2024-09-12 20:37:16 +10:00
rustc_pattern_analysis Revert warning empty patterns as unreachable 2024-09-11 18:36:45 +02:00
rustc_privacy Add warn(unreachable_pub) to rustc_privacy. 2024-08-29 20:18:44 +10:00
rustc_query_impl Add warn(unreachable_pub) to rustc_query_impl. 2024-08-29 20:18:44 +10:00
rustc_query_system chore: Fix typos in 'compiler' (batch 2) 2024-09-02 07:50:22 +02:00
rustc_resolve Auto merge of #129137 - camelid:lazy-def-macro-const, r=BoxyUwU 2024-09-13 01:10:51 +00:00
rustc_sanitizers Also fix if in else 2024-09-11 17:24:01 -04:00
rustc_serialize Auto merge of #129777 - nnethercote:unreachable_pub-4, r=Urgau 2024-09-03 01:27:20 +00:00
rustc_session Auto merge of #130249 - compiler-errors:sad-new-solver-coherence, r=lcnr 2024-09-12 10:17:32 +00:00
rustc_smir Auto merge of #129777 - nnethercote:unreachable_pub-4, r=Urgau 2024-09-03 01:27:20 +00:00
rustc_span Re-enable ConstArgKind::Path lowering by default 2024-09-12 13:56:01 -04:00
rustc_symbol_mangling clippy::useless_conversion 2024-09-11 17:52:53 -04:00
rustc_target Rollup merge of #130235 - compiler-errors:nested-if, r=michaelwoerister 2024-09-12 20:37:16 +10:00
rustc_trait_selection Rollup merge of #130273 - lcnr:overflow-no-constraints, r=compiler-errors 2024-09-12 19:03:43 +02:00
rustc_traits Add warn(unreachable_pub) to rustc_traits. 2024-09-03 08:49:54 +10:00
rustc_transmute update cfgs 2024-09-05 17:24:01 +01:00
rustc_ty_utils Simplify some nested if statements 2024-09-11 13:45:23 -04:00
rustc_type_ir more eagerly discard constraints on overflow 2024-09-12 14:32:44 +02:00
rustc_type_ir_macros
stable_mir chore: Fix typos in 'compiler' (batch 3) 2024-09-02 07:33:41 +02:00