Use `tidy` to sort crate attributes for all compiler crates.
We already do this for a number of crates, e.g. `rustc_middle`,
`rustc_span`, `rustc_metadata`, `rustc_span`, `rustc_errors`.
For the ones we don't, in many cases the attributes are a mess.
- There is no consistency about order of attribute kinds (e.g.
`allow`/`deny`/`feature`).
- Within attribute kind groups (e.g. the `feature` attributes),
sometimes the order is alphabetical, and sometimes there is no
particular order.
- Sometimes the attributes of a particular kind aren't even grouped
all together, e.g. there might be a `feature`, then an `allow`, then
another `feature`.
This commit extends the existing sorting to all compiler crates,
increasing consistency. If any new attribute line is added there is now
only one place it can go -- no need for arbitrary decisions.
Exceptions:
- `rustc_log`, `rustc_next_trait_solver` and `rustc_type_ir_macros`,
because they have no crate attributes.
- `rustc_codegen_gcc`, because it's quasi-external to rustc (e.g. it's
ignored in `rustfmt.toml`).
2024-06-12 13:49:36 +10:00
|
|
|
// tidy-alphabetical-start
|
2021-01-02 14:42:15 +01:00
|
|
|
#![feature(array_windows)]
|
Improve CGU debug printing.
- Add more total and per-CGU measurements.
- Ensure CGUs are sorted by name before the first `debug_dump` calls,
for deterministic output.
- Print items within CGUs in sorted-by-name order, for deterministic
output.
- Add some assertions and comments clarifying sortedness of CGUs at
various points.
An example, before:
```
INITIAL PARTITIONING (5 CodegenUnits, max=29, min=1, max/min=29.0):
CodegenUnit scev95ysd7g4b0z estimated size 2:
- fn <() as std::process::Termination>::report [(External, Hidden)] [h082b15a6d07338dcE] estimated size 2
CodegenUnit 1j0frgtl72rsz24q estimated size 29:
- fn std::rt::lang_start::<()>::{closure#0} [(External, Hidden)] [h695c7b5d6a212565E] estimated size 17
- fn std::rt::lang_start::<()> [(External, Hidden)] [h4ca942948e9cb931E] estimated size 12
CodegenUnit 5dbzi1e5qm0d7kj2 estimated size 4:
- fn <[closure@std::rt::lang_start<()>::{closure#0}] as std::ops::FnOnce<()>>::call_once - shim [(External, Hidden)] [h24eaa44f03b2b233E] estimated size 1
- fn <fn() as std::ops::FnOnce<()>>::call_once - shim(fn()) [(External, Hidden)] [hf338f5339c3711acE] estimated size 1
- fn <[closure@std::rt::lang_start<()>::{closure#0}] as std::ops::FnOnce<()>>::call_once - shim(vtable) [(External, Hidden)] [h595d414cbb7651d5E] estimated size 1
- fn std::ptr::drop_in_place::<[closure@std::rt::lang_start<()>::{closure#0}]> - shim(None) [(External, Hidden)] [h17a19dcdb40600daE] estimated size 1
CodegenUnit 220m1mqa2mlbg7r3 estimated size 1:
- fn main [(External, Hidden)] [hb29587cdb6db5f42E] estimated size 1
CodegenUnit 4ulbh241f7tvyn7x estimated size 6:
- fn std::sys_common::backtrace::__rust_begin_short_backtrace::<fn(), ()> [(External, Hidden)] [h41dada2c21a1259dE] estimated size 6
```
and after:
```
INITIAL PARTITIONING (9 items, total_size=42; 5 CGUs, max_size=29, min_size=1, max_size/min_size=29.0):
- CGU[0] 1j0frgtl72rsz24q (2 items, size=29):
- fn std::rt::lang_start::<()> [(External, Hidden)] [h4ca942948e9cb931E] (size=12)
- fn std::rt::lang_start::<()>::{closure#0} [(External, Hidden)] [h695c7b5d6a212565E] (size=17)
- CGU[1] 220m1mqa2mlbg7r3 (1 items, size=1):
- fn main [(External, Hidden)] [hb29587cdb6db5f42E] (size=1)
- CGU[2] 4ulbh241f7tvyn7x (1 items, size=6):
- fn std::sys_common::backtrace::__rust_begin_short_backtrace::<fn(), ()> [(External, Hidden)] [h41dada2c21a1259dE] (size=6)
- CGU[3] 5dbzi1e5qm0d7kj2 (4 items, size=4):
- fn <[closure@std::rt::lang_start<()>::{closure#0}] as std::ops::FnOnce<()>>::call_once - shim(vtable) [(External, Hidden)] [h595d414cbb7651d5E] (size=1)
- fn <[closure@std::rt::lang_start<()>::{closure#0}] as std::ops::FnOnce<()>>::call_once - shim [(External, Hidden)] [h24eaa44f03b2b233E] (size=1)
- fn <fn() as std::ops::FnOnce<()>>::call_once - shim(fn()) [(External, Hidden)] [hf338f5339c3711acE] (size=1)
- fn std::ptr::drop_in_place::<[closure@std::rt::lang_start<()>::{closure#0}]> - shim(None) [(External, Hidden)] [h17a19dcdb40600daE] (size=1)
- CGU[4] scev95ysd7g4b0z (1 items, size=2):
- fn <() as std::process::Termination>::report [(External, Hidden)] [h082b15a6d07338dcE] (size=2)
```
2023-06-01 10:03:27 +10:00
|
|
|
#![feature(is_sorted)]
|
Use `tidy` to sort crate attributes for all compiler crates.
We already do this for a number of crates, e.g. `rustc_middle`,
`rustc_span`, `rustc_metadata`, `rustc_span`, `rustc_errors`.
For the ones we don't, in many cases the attributes are a mess.
- There is no consistency about order of attribute kinds (e.g.
`allow`/`deny`/`feature`).
- Within attribute kind groups (e.g. the `feature` attributes),
sometimes the order is alphabetical, and sometimes there is no
particular order.
- Sometimes the attributes of a particular kind aren't even grouped
all together, e.g. there might be a `feature`, then an `allow`, then
another `feature`.
This commit extends the existing sorting to all compiler crates,
increasing consistency. If any new attribute line is added there is now
only one place it can go -- no need for arbitrary decisions.
Exceptions:
- `rustc_log`, `rustc_next_trait_solver` and `rustc_type_ir_macros`,
because they have no crate attributes.
- `rustc_codegen_gcc`, because it's quasi-external to rustc (e.g. it's
ignored in `rustfmt.toml`).
2024-06-12 13:49:36 +10:00
|
|
|
// tidy-alphabetical-end
|
2021-01-02 14:42:15 +01:00
|
|
|
|
|
|
|
use rustc_hir::lang_items::LangItem;
|
2024-04-29 13:56:41 +10:00
|
|
|
use rustc_middle::bug;
|
2023-05-16 01:53:21 +02:00
|
|
|
use rustc_middle::query::{Providers, TyCtxtAt};
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::traits;
|
|
|
|
use rustc_middle::ty::adjustment::CustomCoerceUnsized;
|
2024-03-15 14:13:11 -04:00
|
|
|
use rustc_middle::ty::Instance;
|
|
|
|
use rustc_middle::ty::TyCtxt;
|
2022-11-17 14:39:19 +00:00
|
|
|
use rustc_middle::ty::{self, Ty};
|
2024-04-01 20:17:12 -04:00
|
|
|
use rustc_span::def_id::DefId;
|
2024-03-15 14:13:11 -04:00
|
|
|
use rustc_span::def_id::LOCAL_CRATE;
|
2024-01-23 15:23:22 +00:00
|
|
|
use rustc_span::ErrorGuaranteed;
|
2017-10-25 16:14:51 +02:00
|
|
|
|
2021-01-02 14:42:15 +01:00
|
|
|
mod collector;
|
2022-08-18 15:51:47 -06:00
|
|
|
mod errors;
|
2021-01-02 14:42:15 +01:00
|
|
|
mod partitioning;
|
|
|
|
mod polymorphize;
|
|
|
|
mod util;
|
2017-10-25 16:14:51 +02:00
|
|
|
|
2024-03-15 14:13:11 -04:00
|
|
|
use collector::should_codegen_locally;
|
|
|
|
|
2023-11-22 09:53:07 +11:00
|
|
|
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
|
2022-10-13 10:13:02 +01:00
|
|
|
|
2021-01-07 00:41:55 -05:00
|
|
|
fn custom_coerce_unsize_info<'tcx>(
|
2022-11-17 14:39:19 +00:00
|
|
|
tcx: TyCtxtAt<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
source_ty: Ty<'tcx>,
|
|
|
|
target_ty: Ty<'tcx>,
|
2024-01-23 15:23:22 +00:00
|
|
|
) -> Result<CustomCoerceUnsized, ErrorGuaranteed> {
|
2024-05-10 14:59:56 -04:00
|
|
|
let trait_ref = ty::TraitRef::new(
|
2023-04-26 10:55:11 +00:00
|
|
|
tcx.tcx,
|
2024-05-10 14:59:56 -04:00
|
|
|
tcx.require_lang_item(LangItem::CoerceUnsized, Some(tcx.span)),
|
2023-04-25 16:07:48 +00:00
|
|
|
[source_ty, target_ty],
|
2023-07-04 01:18:31 +00:00
|
|
|
);
|
2017-10-25 16:14:51 +02:00
|
|
|
|
2022-09-09 13:36:27 +02:00
|
|
|
match tcx.codegen_select_candidate((ty::ParamEnv::reveal_all(), trait_ref)) {
|
2020-09-24 19:22:36 +02:00
|
|
|
Ok(traits::ImplSource::UserDefined(traits::ImplSourceUserDefinedData {
|
2020-06-02 15:54:24 +00:00
|
|
|
impl_def_id,
|
|
|
|
..
|
2024-01-23 15:23:22 +00:00
|
|
|
})) => Ok(tcx.coerce_unsized_info(impl_def_id)?.custom_kind.unwrap()),
|
2020-05-11 15:25:33 +00:00
|
|
|
impl_source => {
|
|
|
|
bug!("invalid `CoerceUnsized` impl_source: {:?}", impl_source);
|
2017-10-25 16:14:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-02 14:42:15 +01:00
|
|
|
|
2024-03-15 14:13:11 -04:00
|
|
|
/// Returns whether a call from the current crate to the [`Instance`] would produce a call
|
|
|
|
/// from `compiler_builtins` to a symbol the linker must resolve.
|
|
|
|
///
|
|
|
|
/// Such calls from `compiler_bultins` are effectively impossible for the linker to handle. Some
|
|
|
|
/// linkers will optimize such that dead calls to unresolved symbols are not an error, but this is
|
|
|
|
/// not guaranteed. So we used this function in codegen backends to ensure we do not generate any
|
|
|
|
/// unlinkable calls.
|
2024-04-01 20:17:12 -04:00
|
|
|
///
|
|
|
|
/// Note that calls to LLVM intrinsics are uniquely okay because they won't make it to the linker.
|
2024-03-15 14:13:11 -04:00
|
|
|
pub fn is_call_from_compiler_builtins_to_upstream_monomorphization<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
instance: Instance<'tcx>,
|
|
|
|
) -> bool {
|
2024-04-01 20:17:12 -04:00
|
|
|
fn is_llvm_intrinsic(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
|
|
|
|
if let Some(name) = tcx.codegen_fn_attrs(def_id).link_name {
|
|
|
|
name.as_str().starts_with("llvm.")
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let def_id = instance.def_id();
|
|
|
|
!def_id.is_local()
|
2024-03-15 14:13:11 -04:00
|
|
|
&& tcx.is_compiler_builtins(LOCAL_CRATE)
|
2024-04-01 20:17:12 -04:00
|
|
|
&& !is_llvm_intrinsic(tcx, def_id)
|
2024-03-25 13:58:33 -04:00
|
|
|
&& !should_codegen_locally(tcx, instance)
|
2024-03-15 14:13:11 -04:00
|
|
|
}
|
|
|
|
|
2021-01-02 14:42:15 +01:00
|
|
|
pub fn provide(providers: &mut Providers) {
|
|
|
|
partitioning::provide(providers);
|
|
|
|
polymorphize::provide(providers);
|
|
|
|
}
|