rust/compiler
León Orell Valerian Liehr 2a8bb6eda1
Rollup merge of #144218 - Noratrieb:target-spec-json-de-jank, r=fee1-dead
Use serde for target spec json deserialize

The previous manual parsing of `serde_json::Value` was a lot of complicated code and extremely error-prone. It was full of janky behavior like sometimes ignoring type errors, sometimes erroring for type errors, sometimes warning for type errors, and sometimes just ICEing for type errors (the icing on the top).

Additionally, many of the error messages about allowed values were out of date because they were in a completely different place than the FromStr impls. Overall, the system caused confusion for users.

I also found the old deserialization code annoying to read. Whenever a `key!` invocation was found, one had to first look for the right macro arm, and no go to definition could help.

This PR replaces all this manual parsing with a 2-step process involving serde.
First, the string is parsed into a `TargetSpecJson` struct. This struct is a 1:1 representation of the spec JSON. It already parses all the enums and is very simple to read and write.
Then, the fields from this struct are copied into the actual `Target`. The reason for this two-step process instead of just serializing into a `Target` is because of a few reasons

 1. There are a few transformations performed between the two formats
 2. The default logic is implemented this way. Otherwise all the default field values would have to be spelled out again, which is suboptimal. With this logic, they fall out naturally, because everything in the json struct is an `Option`.

Overall, the mapping is pretty simple, with the vast majority of fields just doing a 1:1 mapping that is captured by two macros. I have deliberately avoided making the macros generic to keep them simple.

All the `FromStr` impls now have the error message right inside them, which increases the chance of it being up to date. Some "`from_str`" impls were turned into proper `FromStr` impls to support this.

The new code is much less involved, delegating all the JSON parsing logic to serde, without any manual type matching.

This change introduces a few breaking changes for consumers. While it is possible to use this format on stable, it is very much subject to change, so breaking changes are expected. The hope is also that because of the way stricter behavior, breaking changes are easier to deal with, as they come with clearer error messages.

1. Invalid types now always error, everywhere. Previously, they would sometimes error, and sometimes just be ignored (which meant the users JSON was still broken, just silently!)
2. This now makes use of `deny_unknown_fields` instead of just warning on unused fields, which was done previously. Serde doesn't make it easy to get such warning behavior, which was the primary reason that this now changed. But I think error behavior is very reasonable too. If someone has random stale fields in their JSON, it is likely because these fields did something at some point but no longer do, and the user likely wants to be informed of this so they can figure out what to do.

   This is also relevant for the future. If we remove a field but someone has it set, it probably makes sense for them to take a look whether they need this and should look for alternatives, or whether they can just delete it. Overall, the JSON is made more explicit.

This is the only expected breakage, but there could also be small breakage from small mistakes. All targets roundtrip though, so it can't be anything too major.

fixes rust-lang/rust#144153
2025-07-24 15:08:22 +02:00
..
rustc rename stable_mir to rustc_public, and rustc_smir to rustc_public_bridge 2025-07-14 09:25:54 +00:00
rustc_abi Simplify codegen for niche-encoded variant tests 2025-07-12 04:53:24 -07:00
rustc_arena Introduce Arena::try_alloc_from_iter. 2025-04-19 01:13:18 +00:00
rustc_ast Implement AST visitors using a derive macro. 2025-07-22 01:52:34 +00:00
rustc_ast_ir Use -Wunused_crate_dependencies for compiler crates. 2025-03-20 08:59:43 +11:00
rustc_ast_lowering Rollup merge of #143430 - cjgillot:extra-lifetime-swap, r=oli-obk 2025-07-22 00:54:25 +08:00
rustc_ast_passes AST lowering: More robustly deal with relaxed bounds 2025-07-18 03:13:21 +02:00
rustc_ast_pretty parse const trait Trait 2025-07-17 18:06:26 +08:00
rustc_attr_data_structures Ports #[macro_use] and #[macro_escape] to the new attribute parsing infrastructure 2025-07-23 13:33:23 +02:00
rustc_attr_parsing Ports #[macro_use] and #[macro_escape] to the new attribute parsing infrastructure 2025-07-23 13:33:23 +02:00
rustc_baked_icu_data Add unreachable_pub to RUSTC_LINT_FLAGS for compiler/ crates. 2025-03-11 13:14:21 +11:00
rustc_borrowck Dont ICE on copy error being suppressed due to overflow 2025-07-21 16:07:29 +00:00
rustc_builtin_macros Rollup merge of #143925 - oli-obk:slice-const-partialeq, r=fee1-dead 2025-07-18 14:49:19 +02:00
rustc_codegen_cranelift Remove useless lifetime parameter. 2025-07-23 23:54:37 +00:00
rustc_codegen_gcc Rename tests/assembly into tests/assembly-llvm 2025-07-22 14:27:48 +02:00
rustc_codegen_llvm coverage: Enlarge empty spans during MIR instrumentation, not codegen 2025-07-22 19:49:54 +10:00
rustc_codegen_ssa Remove useless lifetime parameter. 2025-07-23 23:54:37 +00:00
rustc_const_eval Remove const deduplication from the interpreter. 2025-07-23 23:54:37 +00:00
rustc_data_structures Fix wrong cache event query key 2025-07-07 15:16:42 +02:00
rustc_driver Remove recursion_limit increases. 2025-04-02 16:25:27 +11:00
rustc_driver_impl Rollup merge of #143719 - xizheyin:142812-1, r=jieyouxu 2025-07-18 04:27:51 +02:00
rustc_error_codes Update uitest stderrs 2025-07-23 13:33:23 +02:00
rustc_error_messages Remove unused feature gates 2025-06-28 23:36:46 +00:00
rustc_errors rename emit_unless to emit_unless_delay 2025-07-19 01:49:19 +08:00
rustc_expand mbe: Use concrete type for get_unused_rule 2025-07-22 00:36:10 -07:00
rustc_feature Mitigate #[align] name resolution ambiguity regression with a rename 2025-07-19 01:42:30 +08:00
rustc_fluent_macro Remove all unused feature gates from the compiler 2025-06-08 14:50:42 +00:00
rustc_fs_util Retry if creating temp fails with access denied 2025-04-25 11:28:36 +00:00
rustc_graphviz Remove #![warn(unreachable_pub)] from all compiler/ crates. 2025-03-11 13:14:21 +11:00
rustc_hashes Revert "Use workspace lints for crates in compiler/ #138084" 2025-03-10 18:12:47 +08:00
rustc_hir Ports #[macro_use] and #[macro_escape] to the new attribute parsing infrastructure 2025-07-23 13:33:23 +02:00
rustc_hir_analysis Rollup merge of #143373 - cjgillot:bare-unused-trait-imports, r=petrochenkov 2025-07-22 10:26:14 +02:00
rustc_hir_pretty parse const trait Trait 2025-07-17 18:06:26 +08:00
rustc_hir_typeck Rollup merge of #144014 - dianne:edition-guide-links, r=estebank 2025-07-24 15:08:21 +02:00
rustc_incremental Auto merge of #139758 - Zoxc:thread-local-graph, r=oli-obk 2025-05-07 12:39:54 +00:00
rustc_index use div_ceil instead of manual logic 2025-07-05 10:55:42 +02:00
rustc_index_macros In rustc_mir_tranform, iterate over index newtypes instead of ints 2025-04-12 11:53:07 +00:00
rustc_infer Add alias for ArgOutlivesPredicate 2025-07-15 16:02:26 +00:00
rustc_interface Rollup merge of #142097 - ZuseZ4:offload-host1, r=oli-obk 2025-07-22 00:54:24 +08:00
rustc_lexer test(lexer): Add frontmatter unit test 2025-07-10 10:25:29 -05:00
rustc_lint Rollup merge of #144014 - dianne:edition-guide-links, r=estebank 2025-07-24 15:08:21 +02:00
rustc_lint_defs Rollup merge of #144014 - dianne:edition-guide-links, r=estebank 2025-07-24 15:08:21 +02:00
rustc_llvm Rollup merge of #142097 - ZuseZ4:offload-host1, r=oli-obk 2025-07-22 00:54:24 +08:00
rustc_log Add documentation for init_logger_with_additional_layer 2025-06-12 12:11:15 +02:00
rustc_macros Implement AST visitors using a derive macro. 2025-07-22 01:52:34 +00:00
rustc_metadata Rollup merge of #144059 - LorrensP-2158466:remove-crate-loader, r=petrochenkov 2025-07-18 19:14:46 +02:00
rustc_middle Rollup merge of #144094 - saethlin:codegen-the-main-fn, r=petrochenkov 2025-07-24 15:08:21 +02:00
rustc_mir_build Remove useless lifetime parameter. 2025-07-23 23:54:37 +00:00
rustc_mir_dataflow Remove support for SwitchInt edge effects in backward dataflow analyses 2025-07-11 10:56:50 +02:00
rustc_mir_transform Remove useless lifetime parameter. 2025-07-23 23:54:37 +00:00
rustc_monomorphize Rollup merge of #144094 - saethlin:codegen-the-main-fn, r=petrochenkov 2025-07-24 15:08:21 +02:00
rustc_next_trait_solver update comment 2025-07-18 09:36:38 +02:00
rustc_parse Auto merge of #144360 - matthiaskrgr:rollup-b6ej0mm, r=matthiaskrgr 2025-07-23 16:59:13 +00:00
rustc_parse_format update to literal-escaper-0.0.5 2025-07-08 10:16:44 +00:00
rustc_passes Rollup merge of #143374 - cjgillot:bare-extern-crate-map, r=petrochenkov 2025-07-24 15:08:19 +02:00
rustc_pattern_analysis Don't consider unstable fields always-inhabited 2025-07-20 18:23:18 +02:00
rustc_privacy Do not check privacy for RPITIT. 2025-07-17 23:59:41 +00:00
rustc_proc_macro update to literal-escaper-0.0.5 2025-07-08 10:16:44 +00:00
rustc_public Remove useless lifetime parameter. 2025-07-23 23:54:37 +00:00
rustc_public_bridge Remove useless lifetime parameter. 2025-07-23 23:54:37 +00:00
rustc_query_impl Remove unused allow attrs 2025-07-07 12:58:16 +00:00
rustc_query_system use is_multiple_of instead of manual modulo 2025-07-05 10:55:35 +02:00
rustc_resolve Auto merge of #144272 - petrochenkov:disambunder2, r=oli-obk 2025-07-24 09:50:20 +00:00
rustc_sanitizers Remove support for dyn* 2025-07-01 19:00:21 +00:00
rustc_serialize use div_ceil instead of manual logic 2025-07-05 10:55:42 +02:00
rustc_session Rollup merge of #144218 - Noratrieb:target-spec-json-de-jank, r=fee1-dead 2025-07-24 15:08:22 +02:00
rustc_span Rollup merge of #144212 - bjorn3:remove_unique_lang_item, r=oli-obk 2025-07-22 00:54:30 +08:00
rustc_symbol_mangling use codegen_instance_attrs where an instance is (easily) available 2025-07-16 23:24:32 +02:00
rustc_target Rollup merge of #144218 - Noratrieb:target-spec-json-de-jank, r=fee1-dead 2025-07-24 15:08:22 +02:00
rustc_thread_pool Correct comments. 2025-06-28 17:58:21 +08:00
rustc_trait_selection Consider param-env for fast path 2025-07-20 17:45:01 +00:00
rustc_traits Consider param-env for fast path 2025-07-20 17:45:01 +00:00
rustc_transmute compiler: Parse p- specs in datalayout string, allow definition of custom default data address space 2025-07-07 09:04:53 +02:00
rustc_ty_utils Rollup merge of #143793 - fmease:lta-opaq-inf-recur, r=oli-obk 2025-07-17 10:41:45 +02:00
rustc_type_ir Auto merge of #143545 - compiler-errors:coroutine-obl, r=oli-obk 2025-07-18 02:23:50 +00:00
rustc_type_ir_macros Split TypeFolder and FallibleTypeFolder 2025-04-15 18:30:35 +00:00