rust/compiler
Noah Lev 8b75004bca Fix anon const def-creation when macros are involved
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 ideal long-term fix for this is a bit unclear. One option is to delay def
creation 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, this approach has some downsides as well,
and the best approach is yet to be determined.

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`.
2024-09-12 13:48:30 -04:00
..
rustc
rustc_abi
rustc_arena
rustc_ast Fix anon const def-creation when macros are involved 2024-09-12 13:48:30 -04:00
rustc_ast_ir
rustc_ast_lowering Rollup merge of #130235 - compiler-errors:nested-if, r=michaelwoerister 2024-09-12 20:37:16 +10: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
rustc_attr
rustc_baked_icu_data
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
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
rustc_errors clippy::useless_conversion 2024-09-11 17:52:53 -04:00
rustc_expand
rustc_feature
rustc_fluent_macro
rustc_fs_util
rustc_graphviz
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
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
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
rustc_lint Rollup merge of #130235 - compiler-errors:nested-if, r=michaelwoerister 2024-09-12 20:37:16 +10:00
rustc_lint_defs
rustc_llvm
rustc_log
rustc_macros
rustc_metadata
rustc_middle Auto merge of #130269 - Zalathar:rollup-coxzt2t, r=Zalathar 2024-09-12 12:56:55 +00: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
rustc_mir_transform Simplify some nested if statements 2024-09-11 13:45:23 -04: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 #130250 - compiler-errors:useless-conversion, r=jieyouxu 2024-09-12 20:37:17 +10:00
rustc_parse Rollup merge of #130250 - compiler-errors:useless-conversion, r=jieyouxu 2024-09-12 20:37:17 +10:00
rustc_parse_format
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
rustc_query_impl
rustc_query_system
rustc_resolve Fix anon const def-creation when macros are involved 2024-09-12 13:48:30 -04:00
rustc_sanitizers Also fix if in else 2024-09-11 17:24:01 -04:00
rustc_serialize
rustc_session Auto merge of #130249 - compiler-errors:sad-new-solver-coherence, r=lcnr 2024-09-12 10:17:32 +00:00
rustc_smir
rustc_span Rollup merge of #130114 - eduardosm:needless-returns, r=compiler-errors 2024-09-11 15:53:22 -07: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 Auto merge of #130269 - Zalathar:rollup-coxzt2t, r=Zalathar 2024-09-12 12:56:55 +00:00
rustc_traits
rustc_transmute
rustc_ty_utils Simplify some nested if statements 2024-09-11 13:45:23 -04:00
rustc_type_ir Rollup merge of #130094 - workingjubilee:concurrency-is-real, r=lcnr 2024-09-09 19:20:37 -07:00
rustc_type_ir_macros
stable_mir