From 1140e90074b0cbcfdea8535e4b51877e2838227e Mon Sep 17 00:00:00 2001 From: binarycat Date: Tue, 27 May 2025 12:42:25 -0500 Subject: [PATCH 01/47] rustdoc search: prefer stable items in search results fixes https://github.com/rust-lang/rust/issues/138067 --- src/librustdoc/formats/cache.rs | 1 + src/librustdoc/html/render/mod.rs | 9 ++++++++- src/librustdoc/html/render/search_index.rs | 6 ++++++ src/librustdoc/html/static/js/rustdoc.d.ts | 5 ++++- src/librustdoc/html/static/js/search.js | 21 ++++++++++++++++++++- tests/rustdoc-js-std/core-transmute.js | 2 +- tests/rustdoc-js-std/transmute-fail.js | 2 +- tests/rustdoc-js-std/transmute.js | 2 +- tests/rustdoc-js/sort-stability.js | 9 +++++++++ tests/rustdoc-js/sort-stability.rs | 16 ++++++++++++++++ 10 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 tests/rustdoc-js/sort-stability.js create mode 100644 tests/rustdoc-js/sort-stability.rs diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index 4989bd718c9f9..c3251ca780627 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -586,6 +586,7 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It search_type, aliases, deprecation, + stability: item.stability(tcx), }; cache.search_index.push(index_item); } diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 66d5aafa3c1ef..29917bb0fca2a 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -50,7 +50,8 @@ use std::{fs, str}; use askama::Template; use itertools::Either; use rustc_attr_data_structures::{ - ConstStability, DeprecatedSince, Deprecation, RustcVersion, StabilityLevel, StableSince, + ConstStability, DeprecatedSince, Deprecation, RustcVersion, Stability, StabilityLevel, + StableSince, }; use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet}; use rustc_hir::Mutability; @@ -140,6 +141,12 @@ pub(crate) struct IndexItem { pub(crate) search_type: Option, pub(crate) aliases: Box<[Symbol]>, pub(crate) deprecation: Option, + pub(crate) stability: Option, +} +impl IndexItem { + fn is_unstable(&self) -> bool { + matches!(&self.stability, Some(Stability { level: StabilityLevel::Unstable { .. }, .. })) + } } /// A type used for the search index. diff --git a/src/librustdoc/html/render/search_index.rs b/src/librustdoc/html/render/search_index.rs index aff8684ee3a09..6635aa02e97d1 100644 --- a/src/librustdoc/html/render/search_index.rs +++ b/src/librustdoc/html/render/search_index.rs @@ -93,6 +93,7 @@ pub(crate) fn build_index( ), aliases: item.attrs.get_doc_aliases(), deprecation: item.deprecation(tcx), + stability: item.stability(tcx), }); } } @@ -642,6 +643,7 @@ pub(crate) fn build_index( let mut parents_backref_queue = VecDeque::new(); let mut functions = String::with_capacity(self.items.len()); let mut deprecated = Vec::with_capacity(self.items.len()); + let mut unstable = Vec::with_capacity(self.items.len()); let mut type_backref_queue = VecDeque::new(); @@ -698,6 +700,9 @@ pub(crate) fn build_index( // bitmasks always use 1-indexing for items, with 0 as the crate itself deprecated.push(u32::try_from(index + 1).unwrap()); } + if item.is_unstable() { + unstable.push(u32::try_from(index + 1).unwrap()); + } } for (index, path) in &revert_extra_paths { @@ -736,6 +741,7 @@ pub(crate) fn build_index( crate_data.serialize_field("r", &re_exports)?; crate_data.serialize_field("b", &self.associated_item_disambiguators)?; crate_data.serialize_field("c", &bitmap_to_string(&deprecated))?; + crate_data.serialize_field("u", &bitmap_to_string(&unstable))?; crate_data.serialize_field("e", &bitmap_to_string(&self.empty_desc))?; crate_data.serialize_field("P", ¶m_names)?; if has_aliases { diff --git a/src/librustdoc/html/static/js/rustdoc.d.ts b/src/librustdoc/html/static/js/rustdoc.d.ts index 0d2e19e019f34..8110f1f5a5e54 100644 --- a/src/librustdoc/html/static/js/rustdoc.d.ts +++ b/src/librustdoc/html/static/js/rustdoc.d.ts @@ -129,7 +129,7 @@ declare namespace rustdoc { /** * A single parsed "atom" in a search query. For example, - * + * * std::fmt::Formatter, Write -> Result<()> * ┏━━━━━━━━━━━━━━━━━━ ┌──── ┏━━━━━┅┅┅┅┄┄┄┄┄┄┄┄┄┄┄┄┄┄┐ * ┃ │ ┗ QueryElement { ┊ @@ -442,6 +442,8 @@ declare namespace rustdoc { * of `p`) but is used for modules items like free functions. * * `c` is an array of item indices that are deprecated. + * + * `u` is an array of item indices that are unstable. */ type RawSearchIndexCrate = { doc: string, @@ -456,6 +458,7 @@ declare namespace rustdoc { p: Array<[number, string] | [number, string, number] | [number, string, number, number] | [number, string, number, number, string]>, b: Array<[number, String]>, c: string, + u: string, r: Array<[number, number]>, P: Array<[number, string]>, }; diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index dce5fddb3177e..da15433622afe 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -1463,6 +1463,11 @@ class DocSearch { * @type {Map} */ this.searchIndexEmptyDesc = new Map(); + /** + * @type {Map} + */ + this.searchIndexUnstable = new Map(); + /** * @type {Uint32Array} */ @@ -2048,9 +2053,10 @@ class DocSearch { }; const descShardList = [descShard]; - // Deprecated items and items with no description + // Deprecated and unstable items and items with no description this.searchIndexDeprecated.set(crate, new RoaringBitmap(crateCorpus.c)); this.searchIndexEmptyDesc.set(crate, new RoaringBitmap(crateCorpus.e)); + this.searchIndexUnstable.set(crate, new RoaringBitmap(crateCorpus.u)); let descIndex = 0; /** @@ -3284,6 +3290,19 @@ class DocSearch { return a - b; } + // sort unstable items later + a = Number( + // @ts-expect-error + this.searchIndexUnstable.get(aaa.item.crate).contains(aaa.item.bitIndex), + ); + b = Number( + // @ts-expect-error + this.searchIndexUnstable.get(bbb.item.crate).contains(bbb.item.bitIndex), + ); + if (a !== b) { + return a - b; + } + // sort by crate (current crate comes first) a = Number(aaa.item.crate !== preferredCrate); b = Number(bbb.item.crate !== preferredCrate); diff --git a/tests/rustdoc-js-std/core-transmute.js b/tests/rustdoc-js-std/core-transmute.js index 8c9910a32d7f1..b15f398902c31 100644 --- a/tests/rustdoc-js-std/core-transmute.js +++ b/tests/rustdoc-js-std/core-transmute.js @@ -3,9 +3,9 @@ const EXPECTED = [ { 'query': 'generic:T -> generic:U', 'others': [ + { 'path': 'core::mem', 'name': 'transmute' }, { 'path': 'core::intrinsics::simd', 'name': 'simd_as' }, { 'path': 'core::intrinsics::simd', 'name': 'simd_cast' }, - { 'path': 'core::mem', 'name': 'transmute' }, ], }, ]; diff --git a/tests/rustdoc-js-std/transmute-fail.js b/tests/rustdoc-js-std/transmute-fail.js index ddfb276194818..459e8dc3f0029 100644 --- a/tests/rustdoc-js-std/transmute-fail.js +++ b/tests/rustdoc-js-std/transmute-fail.js @@ -6,9 +6,9 @@ const EXPECTED = [ // should-fail tag and the search query below: 'query': 'generic:T -> generic:T', 'others': [ + { 'path': 'std::mem', 'name': 'transmute' }, { 'path': 'std::intrinsics::simd', 'name': 'simd_as' }, { 'path': 'std::intrinsics::simd', 'name': 'simd_cast' }, - { 'path': 'std::mem', 'name': 'transmute' }, ], }, ]; diff --git a/tests/rustdoc-js-std/transmute.js b/tests/rustdoc-js-std/transmute.js index f52e0ab14362d..a85b02e29947e 100644 --- a/tests/rustdoc-js-std/transmute.js +++ b/tests/rustdoc-js-std/transmute.js @@ -5,9 +5,9 @@ const EXPECTED = [ // should-fail tag and the search query below: 'query': 'generic:T -> generic:U', 'others': [ + { 'path': 'std::mem', 'name': 'transmute' }, { 'path': 'std::intrinsics::simd', 'name': 'simd_as' }, { 'path': 'std::intrinsics::simd', 'name': 'simd_cast' }, - { 'path': 'std::mem', 'name': 'transmute' }, ], }, ]; diff --git a/tests/rustdoc-js/sort-stability.js b/tests/rustdoc-js/sort-stability.js new file mode 100644 index 0000000000000..8c095619a0866 --- /dev/null +++ b/tests/rustdoc-js/sort-stability.js @@ -0,0 +1,9 @@ +const EXPECTED = [ + { + 'query': 'foo', + 'others': [ + {"path": "sort_stability::old", "name": "foo"}, + {"path": "sort_stability::new", "name": "foo"}, + ], + }, +]; diff --git a/tests/rustdoc-js/sort-stability.rs b/tests/rustdoc-js/sort-stability.rs new file mode 100644 index 0000000000000..68662bb3aab6c --- /dev/null +++ b/tests/rustdoc-js/sort-stability.rs @@ -0,0 +1,16 @@ +#![feature(staged_api)] +#![stable(feature = "foo_lib", since = "1.0.0")] + +#[stable(feature = "old_foo", since = "1.0.1")] +pub mod old { + /// Old, stable foo + #[stable(feature = "old_foo", since = "1.0.1")] + pub fn foo() {} +} + +#[unstable(feature = "new_foo", issue = "none")] +pub mod new { + /// New, unstable foo + #[unstable(feature = "new_foo", issue = "none")] + pub fn foo() {} +} From 9b107bed9f2d2098dbbaade0ddbc363c651cd38b Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Sun, 27 Jul 2025 21:37:17 -0700 Subject: [PATCH 02/47] Add a mir-opt test for an unneeded drop_in_place --- .../mir-opt/remove_unneeded_drop_in_place.rs | 13 ++++++++++++ ...ce.slice_in_place.RemoveUnneededDrops.diff | 20 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 tests/mir-opt/remove_unneeded_drop_in_place.rs create mode 100644 tests/mir-opt/remove_unneeded_drop_in_place.slice_in_place.RemoveUnneededDrops.diff diff --git a/tests/mir-opt/remove_unneeded_drop_in_place.rs b/tests/mir-opt/remove_unneeded_drop_in_place.rs new file mode 100644 index 0000000000000..7fb7b1e78de0c --- /dev/null +++ b/tests/mir-opt/remove_unneeded_drop_in_place.rs @@ -0,0 +1,13 @@ +//@ test-mir-pass: RemoveUnneededDrops +//@ needs-unwind + +// EMIT_MIR remove_unneeded_drop_in_place.slice_in_place.RemoveUnneededDrops.diff +unsafe fn slice_in_place(ptr: *mut [char]) { + std::ptr::drop_in_place(ptr) +} + +fn main() { + // CHECK-LABEL: fn main( + let mut a = ['o', 'k']; + unsafe { slice_in_place(&raw mut a) }; +} diff --git a/tests/mir-opt/remove_unneeded_drop_in_place.slice_in_place.RemoveUnneededDrops.diff b/tests/mir-opt/remove_unneeded_drop_in_place.slice_in_place.RemoveUnneededDrops.diff new file mode 100644 index 0000000000000..4068e7940bc6d --- /dev/null +++ b/tests/mir-opt/remove_unneeded_drop_in_place.slice_in_place.RemoveUnneededDrops.diff @@ -0,0 +1,20 @@ +- // MIR for `slice_in_place` before RemoveUnneededDrops ++ // MIR for `slice_in_place` after RemoveUnneededDrops + + fn slice_in_place(_1: *mut [char]) -> () { + debug ptr => _1; + let mut _0: (); + let mut _2: *mut [char]; + + bb0: { + StorageLive(_2); + _2 = copy _1; + _0 = drop_in_place::<[char]>(move _2) -> [return: bb1, unwind continue]; + } + + bb1: { + StorageDead(_2); + return; + } + } + From 4e81ecaf3a45a4bb5a8a78ac407d1ef6a7367e72 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Sun, 27 Jul 2025 21:39:56 -0700 Subject: [PATCH 03/47] Let `RemoveUnneededDrops` also remove `drop_in_place` --- .../src/remove_unneeded_drops.rs | 31 ++++++++++++++----- .../mir-opt/remove_unneeded_drop_in_place.rs | 6 +++- ...ce.slice_in_place.RemoveUnneededDrops.diff | 8 ++--- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_mir_transform/src/remove_unneeded_drops.rs b/compiler/rustc_mir_transform/src/remove_unneeded_drops.rs index 43f80508e4a87..6c2dfc59da244 100644 --- a/compiler/rustc_mir_transform/src/remove_unneeded_drops.rs +++ b/compiler/rustc_mir_transform/src/remove_unneeded_drops.rs @@ -4,7 +4,13 @@ //! useful because (unlike MIR building) it runs after type checking, so it can make use of //! `TypingMode::PostAnalysis` to provide more precise type information, especially about opaque //! types. +//! +//! When we're optimizing, we also remove calls to `drop_in_place` when `T` isn't `needs_drop`, +//! as those are essentially equivalent to `Drop` terminators. While the compiler doesn't insert +//! them automatically, preferring the built-in instead, they're common in generic code (such as +//! `Vec::truncate`) so removing them from things like inlined `Vec` is helpful. +use rustc_hir::LangItem; use rustc_middle::mir::*; use rustc_middle::ty::TyCtxt; use tracing::{debug, trace}; @@ -21,15 +27,26 @@ impl<'tcx> crate::MirPass<'tcx> for RemoveUnneededDrops { let mut should_simplify = false; for block in body.basic_blocks.as_mut() { let terminator = block.terminator_mut(); - if let TerminatorKind::Drop { place, target, .. } = terminator.kind { - let ty = place.ty(&body.local_decls, tcx); - if ty.ty.needs_drop(tcx, typing_env) { - continue; + let (ty, target) = match terminator.kind { + TerminatorKind::Drop { place, target, .. } => { + (place.ty(&body.local_decls, tcx).ty, target) + } + TerminatorKind::Call { ref func, target: Some(target), .. } + if tcx.sess.mir_opt_level() > 0 + && let Some((def_id, generics)) = func.const_fn_def() + && tcx.is_lang_item(def_id, LangItem::DropInPlace) => + { + (generics.type_at(0), target) } - debug!("SUCCESS: replacing `drop` with goto({:?})", target); - terminator.kind = TerminatorKind::Goto { target }; - should_simplify = true; + _ => continue, + }; + + if ty.needs_drop(tcx, typing_env) { + continue; } + debug!("SUCCESS: replacing `drop` with goto({:?})", target); + terminator.kind = TerminatorKind::Goto { target }; + should_simplify = true; } // if we applied optimizations, we potentially have some cfg to cleanup to diff --git a/tests/mir-opt/remove_unneeded_drop_in_place.rs b/tests/mir-opt/remove_unneeded_drop_in_place.rs index 7fb7b1e78de0c..470c6499d169c 100644 --- a/tests/mir-opt/remove_unneeded_drop_in_place.rs +++ b/tests/mir-opt/remove_unneeded_drop_in_place.rs @@ -1,13 +1,17 @@ //@ test-mir-pass: RemoveUnneededDrops //@ needs-unwind +//@ compile-flags: -Z mir-opt-level=1 // EMIT_MIR remove_unneeded_drop_in_place.slice_in_place.RemoveUnneededDrops.diff unsafe fn slice_in_place(ptr: *mut [char]) { + // CHECK-LABEL: fn slice_in_place(_1: *mut [char]) + // CHECK: bb0: { + // CHECK-NEXT: return; + // CHECK-NEXT: } std::ptr::drop_in_place(ptr) } fn main() { - // CHECK-LABEL: fn main( let mut a = ['o', 'k']; unsafe { slice_in_place(&raw mut a) }; } diff --git a/tests/mir-opt/remove_unneeded_drop_in_place.slice_in_place.RemoveUnneededDrops.diff b/tests/mir-opt/remove_unneeded_drop_in_place.slice_in_place.RemoveUnneededDrops.diff index 4068e7940bc6d..4d70e7151c393 100644 --- a/tests/mir-opt/remove_unneeded_drop_in_place.slice_in_place.RemoveUnneededDrops.diff +++ b/tests/mir-opt/remove_unneeded_drop_in_place.slice_in_place.RemoveUnneededDrops.diff @@ -9,10 +9,10 @@ bb0: { StorageLive(_2); _2 = copy _1; - _0 = drop_in_place::<[char]>(move _2) -> [return: bb1, unwind continue]; - } - - bb1: { +- _0 = drop_in_place::<[char]>(move _2) -> [return: bb1, unwind continue]; +- } +- +- bb1: { StorageDead(_2); return; } From bcefc2ee977e38a4a5ecd0956e19b0968d33052b Mon Sep 17 00:00:00 2001 From: Amanda Stjerna Date: Wed, 6 Aug 2025 11:12:51 +0200 Subject: [PATCH 04/47] Add annotations to the graphviz region graph on region origins This adds - (ex) for regions whose origin is existential, - (p) for regoins whose origin is a placeholder, and - (p for ) if the originating placeholder is named. This has helped _my_ debugging and it doesn't create too bad clutter, I feel. The change is ridiculously small, but I turned it into a separate PR so we can bikeshed the format. --- .../rustc_borrowck/src/region_infer/graphviz.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_borrowck/src/region_infer/graphviz.rs b/compiler/rustc_borrowck/src/region_infer/graphviz.rs index a3e29982e90fc..899dde9ecd475 100644 --- a/compiler/rustc_borrowck/src/region_infer/graphviz.rs +++ b/compiler/rustc_borrowck/src/region_infer/graphviz.rs @@ -41,7 +41,21 @@ fn render_region_vid<'tcx>( "".to_string() }; - format!("{:?}{universe_str}{external_name_str}", rvid) + let extra_info = match regioncx.region_definition(rvid).origin { + NllRegionVariableOrigin::FreeRegion => "".to_string(), + NllRegionVariableOrigin::Placeholder(p) => match p.bound.kind { + ty::BoundRegionKind::Named(def_id) => { + format!("(p for {})", tcx.item_name(def_id)) + } + ty::BoundRegionKind::ClosureEnv | ty::BoundRegionKind::Anon => "(p)".to_string(), + ty::BoundRegionKind::NamedAnon(_) => { + bug!("only used for pretty printing") + } + }, + NllRegionVariableOrigin::Existential { .. } => "(ex)".to_string(), + }; + + format!("{:?}{universe_str}{external_name_str}{extra_info}", rvid) } impl<'tcx> RegionInferenceContext<'tcx> { From 61e8869ed9aa8bf50a0ea2447a43bf856479feaa Mon Sep 17 00:00:00 2001 From: Amanda Stjerna Date: Wed, 6 Aug 2025 16:35:20 +0200 Subject: [PATCH 05/47] Proposed format --- compiler/rustc_borrowck/src/region_infer/graphviz.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_borrowck/src/region_infer/graphviz.rs b/compiler/rustc_borrowck/src/region_infer/graphviz.rs index 899dde9ecd475..ae4efddea4a9f 100644 --- a/compiler/rustc_borrowck/src/region_infer/graphviz.rs +++ b/compiler/rustc_borrowck/src/region_infer/graphviz.rs @@ -45,14 +45,14 @@ fn render_region_vid<'tcx>( NllRegionVariableOrigin::FreeRegion => "".to_string(), NllRegionVariableOrigin::Placeholder(p) => match p.bound.kind { ty::BoundRegionKind::Named(def_id) => { - format!("(p for {})", tcx.item_name(def_id)) + format!(" (for<{}>)", tcx.item_name(def_id)) } - ty::BoundRegionKind::ClosureEnv | ty::BoundRegionKind::Anon => "(p)".to_string(), + ty::BoundRegionKind::ClosureEnv | ty::BoundRegionKind::Anon => " (for<'_>)".to_string(), ty::BoundRegionKind::NamedAnon(_) => { bug!("only used for pretty printing") } }, - NllRegionVariableOrigin::Existential { .. } => "(ex)".to_string(), + NllRegionVariableOrigin::Existential { .. } => " (ex<'?>)".to_string(), }; format!("{:?}{universe_str}{external_name_str}{extra_info}", rvid) From 648e3fc3938b4b5127e7591187ce523e660ade44 Mon Sep 17 00:00:00 2001 From: Amanda Stjerna Date: Wed, 6 Aug 2025 16:58:42 +0200 Subject: [PATCH 06/47] Track names of existentials --- compiler/rustc_borrowck/src/handle_placeholders.rs | 2 +- compiler/rustc_borrowck/src/region_infer/graphviz.rs | 3 ++- compiler/rustc_borrowck/src/region_infer/mod.rs | 4 ++-- compiler/rustc_borrowck/src/renumber.rs | 2 +- compiler/rustc_borrowck/src/type_check/relate_tys.rs | 2 +- compiler/rustc_infer/src/infer/mod.rs | 1 + 6 files changed, 8 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_borrowck/src/handle_placeholders.rs b/compiler/rustc_borrowck/src/handle_placeholders.rs index aaaf2f45c869d..5eec121b8a735 100644 --- a/compiler/rustc_borrowck/src/handle_placeholders.rs +++ b/compiler/rustc_borrowck/src/handle_placeholders.rs @@ -157,7 +157,7 @@ fn region_definitions<'tcx>( for info in var_infos.iter() { let origin = match info.origin { RegionVariableOrigin::Nll(origin) => origin, - _ => NllRegionVariableOrigin::Existential { from_forall: false }, + _ => NllRegionVariableOrigin::Existential { from_forall: false, name: None }, }; let definition = RegionDefinition { origin, universe: info.universe, external_name: None }; diff --git a/compiler/rustc_borrowck/src/region_infer/graphviz.rs b/compiler/rustc_borrowck/src/region_infer/graphviz.rs index ae4efddea4a9f..b2f67c43125db 100644 --- a/compiler/rustc_borrowck/src/region_infer/graphviz.rs +++ b/compiler/rustc_borrowck/src/region_infer/graphviz.rs @@ -52,7 +52,8 @@ fn render_region_vid<'tcx>( bug!("only used for pretty printing") } }, - NllRegionVariableOrigin::Existential { .. } => " (ex<'?>)".to_string(), + NllRegionVariableOrigin::Existential { name: Some(name), .. } => format!(" (ex<{name}>)"), + NllRegionVariableOrigin::Existential { .. } => format!(" (ex<'_>)"), }; format!("{:?}{universe_str}{external_name_str}{extra_info}", rvid) diff --git a/compiler/rustc_borrowck/src/region_infer/mod.rs b/compiler/rustc_borrowck/src/region_infer/mod.rs index f13200485332f..6ac908fd04c82 100644 --- a/compiler/rustc_borrowck/src/region_infer/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/mod.rs @@ -1940,9 +1940,9 @@ impl<'tcx> RegionInferenceContext<'tcx> { // and here we prefer to blame the source (the y = x statement). let blame_source = match from_region_origin { NllRegionVariableOrigin::FreeRegion - | NllRegionVariableOrigin::Existential { from_forall: false } => true, + | NllRegionVariableOrigin::Existential { from_forall: false, name: _ } => true, NllRegionVariableOrigin::Placeholder(_) - | NllRegionVariableOrigin::Existential { from_forall: true } => false, + | NllRegionVariableOrigin::Existential { from_forall: true, name: _ } => false, }; // To pick a constraint to blame, we organize constraints by how interesting we expect them diff --git a/compiler/rustc_borrowck/src/renumber.rs b/compiler/rustc_borrowck/src/renumber.rs index ff92b4168a86c..100d30704b9e8 100644 --- a/compiler/rustc_borrowck/src/renumber.rs +++ b/compiler/rustc_borrowck/src/renumber.rs @@ -66,7 +66,7 @@ impl<'a, 'tcx> RegionRenumberer<'a, 'tcx> { T: TypeFoldable>, F: Fn() -> RegionCtxt, { - let origin = NllRegionVariableOrigin::Existential { from_forall: false }; + let origin = NllRegionVariableOrigin::Existential { from_forall: false, name: None }; fold_regions(self.infcx.tcx, value, |_region, _depth| { self.infcx.next_nll_region_var(origin, || region_ctxt_fn()) }) diff --git a/compiler/rustc_borrowck/src/type_check/relate_tys.rs b/compiler/rustc_borrowck/src/type_check/relate_tys.rs index bb72d1d52f37e..393adafea0b05 100644 --- a/compiler/rustc_borrowck/src/type_check/relate_tys.rs +++ b/compiler/rustc_borrowck/src/type_check/relate_tys.rs @@ -249,7 +249,7 @@ impl<'a, 'b, 'tcx> NllTypeRelating<'a, 'b, 'tcx> { from_forall: bool, name: Option, ) -> ty::Region<'tcx> { - let origin = NllRegionVariableOrigin::Existential { from_forall }; + let origin = NllRegionVariableOrigin::Existential { name, from_forall }; let reg_var = self.type_checker.infcx.next_nll_region_var(origin, || RegionCtxt::Existential(name)); diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index 7379df7c7c7d6..a2afdc45fa870 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -484,6 +484,7 @@ pub enum NllRegionVariableOrigin { Placeholder(ty::PlaceholderRegion), Existential { + name: Option, /// If this is true, then this variable was created to represent a lifetime /// bound in a `for` binder. For example, it might have been created to /// represent the lifetime `'a` in a type like `for<'a> fn(&'a u32)`. From b0619327ef6afb89492b2fe0bd86f987c39f6c25 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 18 Jul 2025 14:16:08 +0000 Subject: [PATCH 07/47] Change adjust_for_rust_scalar into arg_attrs_for_rust_scalar Directly creating the ArgAttributes rather than adjusting one is a bit clearer. --- compiler/rustc_ty_utils/src/abi.rs | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_ty_utils/src/abi.rs b/compiler/rustc_ty_utils/src/abi.rs index af2e000e34028..a0b333718357f 100644 --- a/compiler/rustc_ty_utils/src/abi.rs +++ b/compiler/rustc_ty_utils/src/abi.rs @@ -268,20 +268,21 @@ fn fn_abi_of_instance<'tcx>( } // Handle safe Rust thin and wide pointers. -fn adjust_for_rust_scalar<'tcx>( +fn arg_attrs_for_rust_scalar<'tcx>( cx: LayoutCx<'tcx>, - attrs: &mut ArgAttributes, scalar: Scalar, layout: TyAndLayout<'tcx>, offset: Size, is_return: bool, drop_target_pointee: Option>, -) { +) -> ArgAttributes { + let mut attrs = ArgAttributes::new(); + // Booleans are always a noundef i1 that needs to be zero-extended. if scalar.is_bool() { attrs.ext(ArgExtension::Zext); attrs.set(ArgAttribute::NoUndef); - return; + return attrs; } if !scalar.is_uninit_valid() { @@ -289,7 +290,7 @@ fn adjust_for_rust_scalar<'tcx>( } // Only pointer types handled below. - let Scalar::Initialized { value: Pointer(_), valid_range } = scalar else { return }; + let Scalar::Initialized { value: Pointer(_), valid_range } = scalar else { return attrs }; // Set `nonnull` if the validity range excludes zero, or for the argument to `drop_in_place`, // which must be nonnull per its documented safety requirements. @@ -358,6 +359,8 @@ fn adjust_for_rust_scalar<'tcx>( } } } + + attrs } /// Ensure that the ABI makes basic sense. @@ -530,17 +533,7 @@ fn fn_abi_new_uncached<'tcx>( }; let mut arg = ArgAbi::new(cx, layout, |layout, scalar, offset| { - let mut attrs = ArgAttributes::new(); - adjust_for_rust_scalar( - *cx, - &mut attrs, - scalar, - *layout, - offset, - is_return, - drop_target_pointee, - ); - attrs + arg_attrs_for_rust_scalar(*cx, scalar, *layout, offset, is_return, drop_target_pointee) }); if arg.layout.is_zst() { From e2acc6c5f4474b3610ce9f1cde84ecb854a354a7 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 18 Jul 2025 14:25:56 +0000 Subject: [PATCH 08/47] Avoid using unadjusted ABI for the thread-local shim This restricts the uses of the unadjusted ABI to LLVM intrinsics. The Rust ABI works fine for the thread-local shim as it always returns pointers directly like the backend expects. --- compiler/rustc_ty_utils/src/abi.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_ty_utils/src/abi.rs b/compiler/rustc_ty_utils/src/abi.rs index a0b333718357f..439d18a69d4d7 100644 --- a/compiler/rustc_ty_utils/src/abi.rs +++ b/compiler/rustc_ty_utils/src/abi.rs @@ -39,7 +39,7 @@ fn fn_sig_for_fn_abi<'tcx>( tcx.thread_local_ptr_ty(instance.def_id()), false, hir::Safety::Safe, - rustc_abi::ExternAbi::Unadjusted, + rustc_abi::ExternAbi::Rust, ); } From 7d88f657e993a3ec394f0b29dea0e5008633a6ba Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Wed, 6 Aug 2025 18:31:58 +0000 Subject: [PATCH 09/47] Fix ICE --- compiler/rustc_ty_utils/src/abi.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_ty_utils/src/abi.rs b/compiler/rustc_ty_utils/src/abi.rs index 439d18a69d4d7..89d1dd8cf231b 100644 --- a/compiler/rustc_ty_utils/src/abi.rs +++ b/compiler/rustc_ty_utils/src/abi.rs @@ -474,9 +474,10 @@ fn fn_abi_new_uncached<'tcx>( let (caller_location, determined_fn_def_id, is_virtual_call) = if let Some(instance) = instance { let is_virtual_call = matches!(instance.def, ty::InstanceKind::Virtual(..)); + let is_tls_shim_call = matches!(instance.def, ty::InstanceKind::ThreadLocalShim(_)); ( instance.def.requires_caller_location(tcx).then(|| tcx.caller_location_ty()), - if is_virtual_call { None } else { Some(instance.def_id()) }, + if is_virtual_call || is_tls_shim_call { None } else { Some(instance.def_id()) }, is_virtual_call, ) } else { @@ -556,6 +557,7 @@ fn fn_abi_new_uncached<'tcx>( c_variadic: sig.c_variadic, fixed_count: inputs.len() as u32, conv, + // FIXME return false for tls shim can_unwind: fn_can_unwind( tcx, // Since `#[rustc_nounwind]` can change unwinding, we cannot infer unwinding by `fn_def_id` for a virtual call. @@ -568,8 +570,9 @@ fn fn_abi_new_uncached<'tcx>( &mut fn_abi, sig.abi, // If this is a virtual call, we cannot pass the `fn_def_id`, as it might call other - // functions from vtable. Internally, `deduced_param_attrs` attempts to infer attributes by - // visit the function body. + // functions from vtable. And for a tls shim, passing the `fn_def_id` would refer to + // the underlying static. Internally, `deduced_param_attrs` attempts to infer attributes + // by visit the function body. determined_fn_def_id, ); debug!("fn_abi_new_uncached = {:?}", fn_abi); From 0bba9bd55c60a108c89cb32971ca2ebbf7386ba7 Mon Sep 17 00:00:00 2001 From: Paul Murphy Date: Wed, 6 Aug 2025 13:37:27 -0500 Subject: [PATCH 10/47] Explicitly disable vector feature on s390x baseline of bad-reg test If the baseline s390x cpu is changed to a newer variant, such as z13, the vector feature may be enabled by default. When rust is packaged on fedora 38 and newer, it is set to z13. Explicitly disable vector support on the baseline test for consistent results across s390x cpus. --- tests/ui/asm/s390x/bad-reg.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ui/asm/s390x/bad-reg.rs b/tests/ui/asm/s390x/bad-reg.rs index 56b2d70937261..eb9138755e76c 100644 --- a/tests/ui/asm/s390x/bad-reg.rs +++ b/tests/ui/asm/s390x/bad-reg.rs @@ -1,7 +1,7 @@ //@ add-core-stubs //@ needs-asm-support //@ revisions: s390x s390x_vector s390x_vector_stable -//@[s390x] compile-flags: --target s390x-unknown-linux-gnu +//@[s390x] compile-flags: --target s390x-unknown-linux-gnu -C target-feature=-vector //@[s390x] needs-llvm-components: systemz //@[s390x_vector] compile-flags: --target s390x-unknown-linux-gnu -C target-feature=+vector //@[s390x_vector] needs-llvm-components: systemz From bf5020937f17fd4c2f8ae5e7703fd9b8d15f0502 Mon Sep 17 00:00:00 2001 From: Karl Meakin Date: Thu, 7 Aug 2025 00:05:22 +0100 Subject: [PATCH 11/47] Optimize `char::is_alphanumeric` Avoid an unnecessary call to `unicode::Alphabetic` when `self` is an ASCII digit (ie `0..=9`). --- library/core/src/char/methods.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index b0752a85faf11..7ee0962721f5b 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -920,7 +920,11 @@ impl char { #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn is_alphanumeric(self) -> bool { - self.is_alphabetic() || self.is_numeric() + if self.is_ascii() { + self.is_ascii_alphanumeric() + } else { + unicode::Alphabetic(self) || unicode::N(self) + } } /// Returns `true` if this `char` has the general category for control codes. From 064d5886544de3f28b0804fb15654ea49eb6b9c7 Mon Sep 17 00:00:00 2001 From: lcnr Date: Thu, 7 Aug 2025 14:20:19 +0200 Subject: [PATCH 12/47] move member-constraints tests --- .../member-constraints/min-choice-reject-ambiguous.rs | 0 .../member-constraints/min-choice-reject-ambiguous.stderr | 0 tests/ui/{nll => impl-trait}/member-constraints/min-choice.rs | 0 .../member-constraints/nested-impl-trait-fail.rs | 0 .../member-constraints/nested-impl-trait-fail.stderr | 0 .../member-constraints/nested-impl-trait-pass.rs | 0 6 files changed, 0 insertions(+), 0 deletions(-) rename tests/ui/{nll => impl-trait}/member-constraints/min-choice-reject-ambiguous.rs (100%) rename tests/ui/{nll => impl-trait}/member-constraints/min-choice-reject-ambiguous.stderr (100%) rename tests/ui/{nll => impl-trait}/member-constraints/min-choice.rs (100%) rename tests/ui/{nll => impl-trait}/member-constraints/nested-impl-trait-fail.rs (100%) rename tests/ui/{nll => impl-trait}/member-constraints/nested-impl-trait-fail.stderr (100%) rename tests/ui/{nll => impl-trait}/member-constraints/nested-impl-trait-pass.rs (100%) diff --git a/tests/ui/nll/member-constraints/min-choice-reject-ambiguous.rs b/tests/ui/impl-trait/member-constraints/min-choice-reject-ambiguous.rs similarity index 100% rename from tests/ui/nll/member-constraints/min-choice-reject-ambiguous.rs rename to tests/ui/impl-trait/member-constraints/min-choice-reject-ambiguous.rs diff --git a/tests/ui/nll/member-constraints/min-choice-reject-ambiguous.stderr b/tests/ui/impl-trait/member-constraints/min-choice-reject-ambiguous.stderr similarity index 100% rename from tests/ui/nll/member-constraints/min-choice-reject-ambiguous.stderr rename to tests/ui/impl-trait/member-constraints/min-choice-reject-ambiguous.stderr diff --git a/tests/ui/nll/member-constraints/min-choice.rs b/tests/ui/impl-trait/member-constraints/min-choice.rs similarity index 100% rename from tests/ui/nll/member-constraints/min-choice.rs rename to tests/ui/impl-trait/member-constraints/min-choice.rs diff --git a/tests/ui/nll/member-constraints/nested-impl-trait-fail.rs b/tests/ui/impl-trait/member-constraints/nested-impl-trait-fail.rs similarity index 100% rename from tests/ui/nll/member-constraints/nested-impl-trait-fail.rs rename to tests/ui/impl-trait/member-constraints/nested-impl-trait-fail.rs diff --git a/tests/ui/nll/member-constraints/nested-impl-trait-fail.stderr b/tests/ui/impl-trait/member-constraints/nested-impl-trait-fail.stderr similarity index 100% rename from tests/ui/nll/member-constraints/nested-impl-trait-fail.stderr rename to tests/ui/impl-trait/member-constraints/nested-impl-trait-fail.stderr diff --git a/tests/ui/nll/member-constraints/nested-impl-trait-pass.rs b/tests/ui/impl-trait/member-constraints/nested-impl-trait-pass.rs similarity index 100% rename from tests/ui/nll/member-constraints/nested-impl-trait-pass.rs rename to tests/ui/impl-trait/member-constraints/nested-impl-trait-pass.rs From 8441f95ec70a2ee5cde4d1b8c81203860e4f5bb9 Mon Sep 17 00:00:00 2001 From: lcnr Date: Thu, 7 Aug 2025 14:29:11 +0200 Subject: [PATCH 13/47] add opaque type member constraint tests --- .../apply_member_constraint-no-req-eq.rs | 24 +++++++++++++ .../incomplete-constraint.rs | 21 ++++++++++++ .../nested-impl-trait-pass.rs | 5 ++- .../reject-choice-due-to-prev-constraint.rs | 34 +++++++++++++++++++ tests/ui/impl-trait/no-anonymize-regions.rs | 18 ++++++++++ 5 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 tests/ui/impl-trait/member-constraints/apply_member_constraint-no-req-eq.rs create mode 100644 tests/ui/impl-trait/member-constraints/incomplete-constraint.rs create mode 100644 tests/ui/impl-trait/member-constraints/reject-choice-due-to-prev-constraint.rs create mode 100644 tests/ui/impl-trait/no-anonymize-regions.rs diff --git a/tests/ui/impl-trait/member-constraints/apply_member_constraint-no-req-eq.rs b/tests/ui/impl-trait/member-constraints/apply_member_constraint-no-req-eq.rs new file mode 100644 index 0000000000000..3aa52fe264472 --- /dev/null +++ b/tests/ui/impl-trait/member-constraints/apply_member_constraint-no-req-eq.rs @@ -0,0 +1,24 @@ +//@ check-pass +// FIXME(-Znext-solver): enable this test + +trait Id { + type This; +} +impl Id for T { + type This = T; +} + +// We have two member constraints here: +// +// - 'unconstrained member ['a, 'static] +// - 'unconstrained member ['static] +// +// Applying the first constraint results in `'unconstrained: 'a` +// while the second then adds `'unconstrained: 'static`. If applying +// member constraints were to require the member region equal to the +// choice region, applying the first constraint first and then the +// second would result in a `'a: 'static` requirement. +fn test<'a>() -> impl Id> + use<'a> { + &() +} +fn main() {} diff --git a/tests/ui/impl-trait/member-constraints/incomplete-constraint.rs b/tests/ui/impl-trait/member-constraints/incomplete-constraint.rs new file mode 100644 index 0000000000000..92fd31516d72d --- /dev/null +++ b/tests/ui/impl-trait/member-constraints/incomplete-constraint.rs @@ -0,0 +1,21 @@ +//@ check-pass +// FIXME(-Znext-solver): enable this test + +// These functions currently does not normalize the opaque type but will do +// so in the future. At this point we've got a new use of the opaque will fully +// universal arguments but for which lifetimes in the hidden type are unconstrained. +// +// Applying the member constraints would then incompletely infer `'unconstrained` to `'static`. +fn new_defining_use R, T, R>(_: F) {} + +fn rpit1<'a, 'b: 'b>(x: &'b ()) -> impl Sized + use<'a, 'b> { + new_defining_use(rpit1::<'a, 'b>); + x +} + +struct Inv<'a, 'b>(*mut (&'a (), &'b ())); +fn rpit2<'a>(_: ()) -> impl Sized + use<'a> { + new_defining_use(rpit2::<'a>); + Inv::<'a, 'static>(std::ptr::null_mut()) +} +fn main() {} diff --git a/tests/ui/impl-trait/member-constraints/nested-impl-trait-pass.rs b/tests/ui/impl-trait/member-constraints/nested-impl-trait-pass.rs index 4633ad6823024..6860fc5e6a532 100644 --- a/tests/ui/impl-trait/member-constraints/nested-impl-trait-pass.rs +++ b/tests/ui/impl-trait/member-constraints/nested-impl-trait-pass.rs @@ -1,6 +1,7 @@ // Nested impl-traits can impose different member constraints on the same region variable. //@ check-pass +// FIXME(-Znext-solver): enable this test trait Cap<'a> {} impl Cap<'_> for T {} @@ -8,7 +9,9 @@ impl Cap<'_> for T {} // Assuming the hidden type is `[&'?15 u8; 1]`, we have two distinct member constraints: // - '?15 member ['static, 'a, 'b] // from outer impl-trait // - '?15 member ['static, 'a] // from inner impl-trait -// To satisfy both we can only choose 'a. +// To satisfy both we can only choose 'a. Concretely, first member constraint requires ?15 +// to outlive at least 'b while the second requires ?15 to outlive 'a. As 'a outlives 'b we +// end up with 'a as the final member region. fn pass_early_bound<'s, 'a, 'b>(a: &'s u8) -> impl IntoIterator> + Cap<'b> where 's: 'a, diff --git a/tests/ui/impl-trait/member-constraints/reject-choice-due-to-prev-constraint.rs b/tests/ui/impl-trait/member-constraints/reject-choice-due-to-prev-constraint.rs new file mode 100644 index 0000000000000..4018256b2c84e --- /dev/null +++ b/tests/ui/impl-trait/member-constraints/reject-choice-due-to-prev-constraint.rs @@ -0,0 +1,34 @@ +//@ revisions: current next +//@[next] compile-flags: -Znext-solver +//@ ignore-compare-mode-next-solver (explicit revisions) +//@ check-pass + +// We've got `'0 member ['a, 'b, 'static]` and `'1 member ['a, 'b, 'static]`. +// +// As '0 gets outlived by 'a - its "upper bound" - the only applicable choice +// region is 'a. +// +// '1 has to outlive 'b so the only applicabel choice regions are 'b and 'static. +// Considering this member constraint by itself would choose 'b as it is the +// smaller of the two regions. +// +// However, this is only the case when ignoring the member constraint on '0. +// After applying this constraint and requiring '0 to outlive 'a. As '1 outlives +// '0, the region 'b is no longer an applicable choice region for '1 as 'b does +// does not outlive 'a. We would therefore choose 'static. +// +// This means applying member constraints is order dependent. We handle this by +// first applying member constraints for regions 'x and then consider the resulting +// constraints when applying member constraints for regions 'y with 'y: 'x. +fn with_constraints<'r0, 'r1, 'a, 'b>() -> *mut (&'r0 (), &'r1 ()) +where + 'r1: 'r0, + 'a: 'r0, + 'r1: 'b, +{ + loop {} +} +fn foo<'a, 'b>() -> impl Sized + use<'a, 'b> { + with_constraints::<'_, '_, 'a, 'b>() +} +fn main() {} diff --git a/tests/ui/impl-trait/no-anonymize-regions.rs b/tests/ui/impl-trait/no-anonymize-regions.rs new file mode 100644 index 0000000000000..4f7f7c0641c65 --- /dev/null +++ b/tests/ui/impl-trait/no-anonymize-regions.rs @@ -0,0 +1,18 @@ +//@ check-pass +// FIXME(-Znext-solver): enable this test + +// A regression test for an error in `redis` while working on #139587. +// +// We check for structural equality when adding defining uses of opaques. +// In this test one defining use had anonymized regions while the other +// one did not, causing an error. +struct W(T); +fn constrain R, T, R>(f: F) -> R { + loop {} +} +fn foo<'a>(x: for<'b> fn(&'b ())) -> impl Sized + use<'a> { + let mut r = constrain(foo::<'_>); + r = W(x); + r +} +fn main() {} From b60f75c9261e0968fbb6f3aa435660db189e0ab5 Mon Sep 17 00:00:00 2001 From: George Tokmaji Date: Sat, 31 May 2025 21:18:41 +0200 Subject: [PATCH 14/47] Add diagnostic explaining STATUS_STACK_BUFFER_OVERRUN not only being used for stack buffer overruns if link.exe exits with that exit code `STATUS_STACK_BUFFER_OVERRUN` is also used for fast abnormal program termination, e.g. by abort(). Emit a special diagnostic to let people know that this most likely doesn't indicate a stack buffer overrun. --- compiler/rustc_codegen_ssa/messages.ftl | 3 +++ compiler/rustc_codegen_ssa/src/back/link.rs | 8 ++++++++ compiler/rustc_codegen_ssa/src/errors.rs | 5 +++++ 3 files changed, 16 insertions(+) diff --git a/compiler/rustc_codegen_ssa/messages.ftl b/compiler/rustc_codegen_ssa/messages.ftl index a70d0011d161f..53a52736a97c3 100644 --- a/compiler/rustc_codegen_ssa/messages.ftl +++ b/compiler/rustc_codegen_ssa/messages.ftl @@ -178,6 +178,9 @@ codegen_ssa_ld64_unimplemented_modifier = `as-needed` modifier not implemented y codegen_ssa_lib_def_write_failure = failed to write lib.def file: {$error} +codegen_ssa_link_exe_status_stack_buffer_overrun = 0xc0000409 is `STATUS_STACK_BUFFER_OVERRUN` + .note = this may have been caused by a program abort and not a stack buffer overrun + codegen_ssa_link_exe_unexpected_error = `link.exe` returned an unexpected error codegen_ssa_link_script_unavailable = can only use link script when linking with GNU-like linker diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index b69fbf61185d7..c82af3d9d1e2b 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -880,6 +880,14 @@ fn link_natively( windows_registry::find_tool(&sess.target.arch, "link.exe").is_some(); sess.dcx().emit_note(errors::LinkExeUnexpectedError); + + // STATUS_STACK_BUFFER_OVERRUN is also used for fast abnormal program termination, e.g. abort(). + // Emit a special diagnostic to let people know that this most likely doesn't indicate a stack buffer overrun. + const STATUS_STACK_BUFFER_OVERRUN: i32 = 0xc0000409u32 as _; + if code == STATUS_STACK_BUFFER_OVERRUN { + sess.dcx().emit_note(errors::LinkExeStatusStackBufferOverrun); + } + if is_vs_installed && has_linker { // the linker is broken sess.dcx().emit_note(errors::RepairVSBuildTools); diff --git a/compiler/rustc_codegen_ssa/src/errors.rs b/compiler/rustc_codegen_ssa/src/errors.rs index 3d787d8bdbde9..9ef1473e538ea 100644 --- a/compiler/rustc_codegen_ssa/src/errors.rs +++ b/compiler/rustc_codegen_ssa/src/errors.rs @@ -550,6 +550,11 @@ impl Diagnostic<'_, G> for LinkingFailed<'_> { #[diag(codegen_ssa_link_exe_unexpected_error)] pub(crate) struct LinkExeUnexpectedError; +#[derive(Diagnostic)] +#[diag(codegen_ssa_link_exe_status_stack_buffer_overrun)] +#[note] +pub(crate) struct LinkExeStatusStackBufferOverrun; + #[derive(Diagnostic)] #[diag(codegen_ssa_repair_vs_build_tools)] pub(crate) struct RepairVSBuildTools; From 7e5acb91d70d2d6f77ed4e12041ef00a1e4582fe Mon Sep 17 00:00:00 2001 From: George Tokmaji Date: Thu, 7 Aug 2025 23:42:17 +0200 Subject: [PATCH 15/47] Add note mentioning the event log to LinkExeStatusStackBufferOverrun --- compiler/rustc_codegen_ssa/messages.ftl | 3 ++- compiler/rustc_codegen_ssa/src/errors.rs | 13 ++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_codegen_ssa/messages.ftl b/compiler/rustc_codegen_ssa/messages.ftl index 53a52736a97c3..71212253afc6e 100644 --- a/compiler/rustc_codegen_ssa/messages.ftl +++ b/compiler/rustc_codegen_ssa/messages.ftl @@ -179,7 +179,8 @@ codegen_ssa_ld64_unimplemented_modifier = `as-needed` modifier not implemented y codegen_ssa_lib_def_write_failure = failed to write lib.def file: {$error} codegen_ssa_link_exe_status_stack_buffer_overrun = 0xc0000409 is `STATUS_STACK_BUFFER_OVERRUN` - .note = this may have been caused by a program abort and not a stack buffer overrun + .abort_note = this may have been caused by a program abort and not a stack buffer overrun + .event_log_note = consider checking the Application Event Log for Windows Error Reporting events to see the fail fast error code codegen_ssa_link_exe_unexpected_error = `link.exe` returned an unexpected error diff --git a/compiler/rustc_codegen_ssa/src/errors.rs b/compiler/rustc_codegen_ssa/src/errors.rs index 9ef1473e538ea..fc1092f79a5da 100644 --- a/compiler/rustc_codegen_ssa/src/errors.rs +++ b/compiler/rustc_codegen_ssa/src/errors.rs @@ -550,11 +550,18 @@ impl Diagnostic<'_, G> for LinkingFailed<'_> { #[diag(codegen_ssa_link_exe_unexpected_error)] pub(crate) struct LinkExeUnexpectedError; -#[derive(Diagnostic)] -#[diag(codegen_ssa_link_exe_status_stack_buffer_overrun)] -#[note] pub(crate) struct LinkExeStatusStackBufferOverrun; +impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for LinkExeStatusStackBufferOverrun { + fn into_diag(self, dcx: rustc_errors::DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G> { + let mut diag = + Diag::new(dcx, level, fluent::codegen_ssa_link_exe_status_stack_buffer_overrun); + diag.note(fluent::codegen_ssa_abort_note); + diag.note(fluent::codegen_ssa_event_log_note); + diag + } +} + #[derive(Diagnostic)] #[diag(codegen_ssa_repair_vs_build_tools)] pub(crate) struct RepairVSBuildTools; From c00881bcb9a02906fbbfff9fe447053914a273ee Mon Sep 17 00:00:00 2001 From: Manuel Drehwald Date: Thu, 7 Aug 2025 16:31:00 -0700 Subject: [PATCH 16/47] update enzyme submodule to handle llvm 21 --- src/bootstrap/src/core/build_steps/llvm.rs | 1 + src/tools/enzyme | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/bootstrap/src/core/build_steps/llvm.rs b/src/bootstrap/src/core/build_steps/llvm.rs index 721ba6ca459e4..065a39a4e7142 100644 --- a/src/bootstrap/src/core/build_steps/llvm.rs +++ b/src/bootstrap/src/core/build_steps/llvm.rs @@ -986,6 +986,7 @@ impl Step for Enzyme { .env("LLVM_CONFIG_REAL", &llvm_config) .define("LLVM_ENABLE_ASSERTIONS", "ON") .define("ENZYME_EXTERNAL_SHARED_LIB", "ON") + .define("ENZYME_BC_LOADER", "OFF") .define("LLVM_DIR", builder.llvm_out(target)); cfg.build(); diff --git a/src/tools/enzyme b/src/tools/enzyme index 2cccfba93c165..58af4e9e6c047 160000 --- a/src/tools/enzyme +++ b/src/tools/enzyme @@ -1 +1 @@ -Subproject commit 2cccfba93c1650f26f1cf8be8aa875a7c1d23fb3 +Subproject commit 58af4e9e6c047534ba059b12af17cecb8a2e9f9e From b4a3d3014e128fcc8dd0e11136929863a3768cae Mon Sep 17 00:00:00 2001 From: Roger Curley Date: Sun, 13 Jul 2025 22:04:06 -0400 Subject: [PATCH 17/47] Consolidate abs tests This clobbers the existing generic abs test, but it covers strictly more, so that seems fine. --- library/coretests/tests/floats/f128.rs | 13 ------------- library/coretests/tests/floats/f16.rs | 13 ------------- library/coretests/tests/floats/f32.rs | 12 ------------ library/coretests/tests/floats/f64.rs | 12 ------------ library/coretests/tests/floats/mod.rs | 12 +++++++++--- 5 files changed, 9 insertions(+), 53 deletions(-) diff --git a/library/coretests/tests/floats/f128.rs b/library/coretests/tests/floats/f128.rs index 36d6a20a94427..bd064443b2a91 100644 --- a/library/coretests/tests/floats/f128.rs +++ b/library/coretests/tests/floats/f128.rs @@ -39,19 +39,6 @@ const NAN_MASK2: u128 = 0x00005555555555555555555555555555; // FIXME(f16_f128,miri): many of these have to be disabled since miri does not yet support // the intrinsics. -#[test] -#[cfg(any(miri, target_has_reliable_f128_math))] -fn test_abs() { - assert_biteq!(f128::INFINITY.abs(), f128::INFINITY); - assert_biteq!(1f128.abs(), 1f128); - assert_biteq!(0f128.abs(), 0f128); - assert_biteq!((-0f128).abs(), 0f128); - assert_biteq!((-1f128).abs(), 1f128); - assert_biteq!(f128::NEG_INFINITY.abs(), f128::INFINITY); - assert_biteq!((1f128 / f128::NEG_INFINITY).abs(), 0f128); - assert!(f128::NAN.abs().is_nan()); -} - #[test] fn test_is_sign_positive() { assert!(f128::INFINITY.is_sign_positive()); diff --git a/library/coretests/tests/floats/f16.rs b/library/coretests/tests/floats/f16.rs index 351c008a37bab..11b4a38a7ed89 100644 --- a/library/coretests/tests/floats/f16.rs +++ b/library/coretests/tests/floats/f16.rs @@ -45,19 +45,6 @@ const NAN_MASK2: u16 = 0x0155; // FIXME(f16_f128,miri): many of these have to be disabled since miri does not yet support // the intrinsics. -#[test] -#[cfg(any(miri, target_has_reliable_f16_math))] -fn test_abs() { - assert_biteq!(f16::INFINITY.abs(), f16::INFINITY); - assert_biteq!(1f16.abs(), 1f16); - assert_biteq!(0f16.abs(), 0f16); - assert_biteq!((-0f16).abs(), 0f16); - assert_biteq!((-1f16).abs(), 1f16); - assert_biteq!(f16::NEG_INFINITY.abs(), f16::INFINITY); - assert_biteq!((1f16 / f16::NEG_INFINITY).abs(), 0f16); - assert!(f16::NAN.abs().is_nan()); -} - #[test] fn test_is_sign_positive() { assert!(f16::INFINITY.is_sign_positive()); diff --git a/library/coretests/tests/floats/f32.rs b/library/coretests/tests/floats/f32.rs index 267b0e4e29434..ab68b9dbfbec0 100644 --- a/library/coretests/tests/floats/f32.rs +++ b/library/coretests/tests/floats/f32.rs @@ -29,18 +29,6 @@ const NAN_MASK2: u32 = 0x0055_5555; /// They serve as a way to get an idea of the real precision of floating point operations on different platforms. const APPROX_DELTA: f32 = if cfg!(miri) { 1e-4 } else { 1e-6 }; -#[test] -fn test_abs() { - assert_biteq!(f32::INFINITY.abs(), f32::INFINITY); - assert_biteq!(1f32.abs(), 1f32); - assert_biteq!(0f32.abs(), 0f32); - assert_biteq!((-0f32).abs(), 0f32); - assert_biteq!((-1f32).abs(), 1f32); - assert_biteq!(f32::NEG_INFINITY.abs(), f32::INFINITY); - assert_biteq!((1f32 / f32::NEG_INFINITY).abs(), 0f32); - assert!(f32::NAN.abs().is_nan()); -} - #[test] fn test_signum() { assert_biteq!(f32::INFINITY.signum(), 1f32); diff --git a/library/coretests/tests/floats/f64.rs b/library/coretests/tests/floats/f64.rs index 735b7a7651519..05c2df085d52e 100644 --- a/library/coretests/tests/floats/f64.rs +++ b/library/coretests/tests/floats/f64.rs @@ -24,18 +24,6 @@ const NAN_MASK1: u64 = 0x000a_aaaa_aaaa_aaaa; /// Second pattern over the mantissa const NAN_MASK2: u64 = 0x0005_5555_5555_5555; -#[test] -fn test_abs() { - assert_biteq!(f64::INFINITY.abs(), f64::INFINITY); - assert_biteq!(1f64.abs(), 1f64); - assert_biteq!(0f64.abs(), 0f64); - assert_biteq!((-0f64).abs(), 0f64); - assert_biteq!((-1f64).abs(), 1f64); - assert_biteq!(f64::NEG_INFINITY.abs(), f64::INFINITY); - assert_biteq!((1f64 / f64::NEG_INFINITY).abs(), 0f64); - assert!(f64::NAN.abs().is_nan()); -} - #[test] fn test_signum() { assert_biteq!(f64::INFINITY.signum(), 1f64); diff --git a/library/coretests/tests/floats/mod.rs b/library/coretests/tests/floats/mod.rs index 43431bba6954b..abfcfdd99911f 100644 --- a/library/coretests/tests/floats/mod.rs +++ b/library/coretests/tests/floats/mod.rs @@ -720,10 +720,16 @@ float_test! { f128: #[cfg(any(miri, target_has_reliable_f128_math))], }, test { - assert_biteq!((-1.0 as Float).abs(), 1.0); - assert_biteq!((1.0 as Float).abs(), 1.0); - assert_biteq!(Float::NEG_INFINITY.abs(), Float::INFINITY); + let one: Float = 1.0; + let zero: Float = 0.0; assert_biteq!(Float::INFINITY.abs(), Float::INFINITY); + assert_biteq!(one.abs(), one); + assert_biteq!(zero.abs(), zero); + assert_biteq!((-zero).abs(), zero); + assert_biteq!((-one).abs(), one); + assert_biteq!(Float::NEG_INFINITY.abs(), Float::INFINITY); + assert_biteq!((one / Float::NEG_INFINITY).abs(), zero); + assert!(Float::NAN.abs().is_nan()); } } From 83878ea2286f2b2e21411d134f390b31bbfaec7b Mon Sep 17 00:00:00 2001 From: Roger Curley Date: Sun, 13 Jul 2025 22:36:56 -0400 Subject: [PATCH 18/47] Consolidate signum tests --- library/coretests/tests/floats/f32.rs | 12 ------------ library/coretests/tests/floats/f64.rs | 12 ------------ library/coretests/tests/floats/mod.rs | 20 ++++++++++++++++++++ 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/library/coretests/tests/floats/f32.rs b/library/coretests/tests/floats/f32.rs index ab68b9dbfbec0..23af02cada0d6 100644 --- a/library/coretests/tests/floats/f32.rs +++ b/library/coretests/tests/floats/f32.rs @@ -29,18 +29,6 @@ const NAN_MASK2: u32 = 0x0055_5555; /// They serve as a way to get an idea of the real precision of floating point operations on different platforms. const APPROX_DELTA: f32 = if cfg!(miri) { 1e-4 } else { 1e-6 }; -#[test] -fn test_signum() { - assert_biteq!(f32::INFINITY.signum(), 1f32); - assert_biteq!(1f32.signum(), 1f32); - assert_biteq!(0f32.signum(), 1f32); - assert_biteq!((-0f32).signum(), -1f32); - assert_biteq!((-1f32).signum(), -1f32); - assert_biteq!(f32::NEG_INFINITY.signum(), -1f32); - assert_biteq!((1f32 / f32::NEG_INFINITY).signum(), -1f32); - assert!(f32::NAN.signum().is_nan()); -} - #[test] fn test_is_sign_positive() { assert!(f32::INFINITY.is_sign_positive()); diff --git a/library/coretests/tests/floats/f64.rs b/library/coretests/tests/floats/f64.rs index 05c2df085d52e..cc3311aaa7be5 100644 --- a/library/coretests/tests/floats/f64.rs +++ b/library/coretests/tests/floats/f64.rs @@ -24,18 +24,6 @@ const NAN_MASK1: u64 = 0x000a_aaaa_aaaa_aaaa; /// Second pattern over the mantissa const NAN_MASK2: u64 = 0x0005_5555_5555_5555; -#[test] -fn test_signum() { - assert_biteq!(f64::INFINITY.signum(), 1f64); - assert_biteq!(1f64.signum(), 1f64); - assert_biteq!(0f64.signum(), 1f64); - assert_biteq!((-0f64).signum(), -1f64); - assert_biteq!((-1f64).signum(), -1f64); - assert_biteq!(f64::NEG_INFINITY.signum(), -1f64); - assert_biteq!((1f64 / f64::NEG_INFINITY).signum(), -1f64); - assert!(f64::NAN.signum().is_nan()); -} - #[test] fn test_is_sign_positive() { assert!(f64::INFINITY.is_sign_positive()); diff --git a/library/coretests/tests/floats/mod.rs b/library/coretests/tests/floats/mod.rs index abfcfdd99911f..26735bbeb0d2f 100644 --- a/library/coretests/tests/floats/mod.rs +++ b/library/coretests/tests/floats/mod.rs @@ -957,3 +957,23 @@ float_test! { assert!(Float::NEG_INFINITY.fract().is_nan()); } } + +float_test! { + name: signum, + attrs: { + f16: #[cfg(any(miri, target_has_reliable_f16_math))], + f128: #[cfg(any(miri, target_has_reliable_f128_math))], + }, + test { + let one: Float = 1.0; + let zero: Float = 0.0; + assert_biteq!(Float::INFINITY.signum(), one); + assert_biteq!(one.signum(), one); + assert_biteq!(zero.signum(), one); + assert_biteq!((-zero).signum(), -one); + assert_biteq!((-one).signum(), -one); + assert_biteq!(Float::NEG_INFINITY.signum(), -one); + assert_biteq!((one / Float::NEG_INFINITY).signum(), -one); + assert!(Float::NAN.signum().is_nan()); + } +} From 951ef57b85bfa119b34378fdd55bdabd6d22e6d6 Mon Sep 17 00:00:00 2001 From: Roger Curley Date: Sun, 13 Jul 2025 22:46:18 -0400 Subject: [PATCH 19/47] Consolidate is_positive tests --- library/coretests/tests/floats/f128.rs | 13 ------------- library/coretests/tests/floats/f16.rs | 13 ------------- library/coretests/tests/floats/f32.rs | 13 ------------- library/coretests/tests/floats/f64.rs | 13 ------------- library/coretests/tests/floats/mod.rs | 21 +++++++++++++++++++++ 5 files changed, 21 insertions(+), 52 deletions(-) diff --git a/library/coretests/tests/floats/f128.rs b/library/coretests/tests/floats/f128.rs index bd064443b2a91..d67cae19bb794 100644 --- a/library/coretests/tests/floats/f128.rs +++ b/library/coretests/tests/floats/f128.rs @@ -39,19 +39,6 @@ const NAN_MASK2: u128 = 0x00005555555555555555555555555555; // FIXME(f16_f128,miri): many of these have to be disabled since miri does not yet support // the intrinsics. -#[test] -fn test_is_sign_positive() { - assert!(f128::INFINITY.is_sign_positive()); - assert!(1f128.is_sign_positive()); - assert!(0f128.is_sign_positive()); - assert!(!(-0f128).is_sign_positive()); - assert!(!(-1f128).is_sign_positive()); - assert!(!f128::NEG_INFINITY.is_sign_positive()); - assert!(!(1f128 / f128::NEG_INFINITY).is_sign_positive()); - assert!(f128::NAN.is_sign_positive()); - assert!(!(-f128::NAN).is_sign_positive()); -} - #[test] fn test_is_sign_negative() { assert!(!f128::INFINITY.is_sign_negative()); diff --git a/library/coretests/tests/floats/f16.rs b/library/coretests/tests/floats/f16.rs index 11b4a38a7ed89..9ec43f012a2c3 100644 --- a/library/coretests/tests/floats/f16.rs +++ b/library/coretests/tests/floats/f16.rs @@ -45,19 +45,6 @@ const NAN_MASK2: u16 = 0x0155; // FIXME(f16_f128,miri): many of these have to be disabled since miri does not yet support // the intrinsics. -#[test] -fn test_is_sign_positive() { - assert!(f16::INFINITY.is_sign_positive()); - assert!(1f16.is_sign_positive()); - assert!(0f16.is_sign_positive()); - assert!(!(-0f16).is_sign_positive()); - assert!(!(-1f16).is_sign_positive()); - assert!(!f16::NEG_INFINITY.is_sign_positive()); - assert!(!(1f16 / f16::NEG_INFINITY).is_sign_positive()); - assert!(f16::NAN.is_sign_positive()); - assert!(!(-f16::NAN).is_sign_positive()); -} - #[test] fn test_is_sign_negative() { assert!(!f16::INFINITY.is_sign_negative()); diff --git a/library/coretests/tests/floats/f32.rs b/library/coretests/tests/floats/f32.rs index 23af02cada0d6..c45974382501a 100644 --- a/library/coretests/tests/floats/f32.rs +++ b/library/coretests/tests/floats/f32.rs @@ -29,19 +29,6 @@ const NAN_MASK2: u32 = 0x0055_5555; /// They serve as a way to get an idea of the real precision of floating point operations on different platforms. const APPROX_DELTA: f32 = if cfg!(miri) { 1e-4 } else { 1e-6 }; -#[test] -fn test_is_sign_positive() { - assert!(f32::INFINITY.is_sign_positive()); - assert!(1f32.is_sign_positive()); - assert!(0f32.is_sign_positive()); - assert!(!(-0f32).is_sign_positive()); - assert!(!(-1f32).is_sign_positive()); - assert!(!f32::NEG_INFINITY.is_sign_positive()); - assert!(!(1f32 / f32::NEG_INFINITY).is_sign_positive()); - assert!(f32::NAN.is_sign_positive()); - assert!(!(-f32::NAN).is_sign_positive()); -} - #[test] fn test_is_sign_negative() { assert!(!f32::INFINITY.is_sign_negative()); diff --git a/library/coretests/tests/floats/f64.rs b/library/coretests/tests/floats/f64.rs index cc3311aaa7be5..d20f45d872d8a 100644 --- a/library/coretests/tests/floats/f64.rs +++ b/library/coretests/tests/floats/f64.rs @@ -24,19 +24,6 @@ const NAN_MASK1: u64 = 0x000a_aaaa_aaaa_aaaa; /// Second pattern over the mantissa const NAN_MASK2: u64 = 0x0005_5555_5555_5555; -#[test] -fn test_is_sign_positive() { - assert!(f64::INFINITY.is_sign_positive()); - assert!(1f64.is_sign_positive()); - assert!(0f64.is_sign_positive()); - assert!(!(-0f64).is_sign_positive()); - assert!(!(-1f64).is_sign_positive()); - assert!(!f64::NEG_INFINITY.is_sign_positive()); - assert!(!(1f64 / f64::NEG_INFINITY).is_sign_positive()); - assert!(f64::NAN.is_sign_positive()); - assert!(!(-f64::NAN).is_sign_positive()); -} - #[test] fn test_is_sign_negative() { assert!(!f64::INFINITY.is_sign_negative()); diff --git a/library/coretests/tests/floats/mod.rs b/library/coretests/tests/floats/mod.rs index 26735bbeb0d2f..d45cc73174a28 100644 --- a/library/coretests/tests/floats/mod.rs +++ b/library/coretests/tests/floats/mod.rs @@ -977,3 +977,24 @@ float_test! { assert!(Float::NAN.signum().is_nan()); } } + +float_test! { + name: is_sign_positive, + attrs: { + f16: #[cfg(any(miri, target_has_reliable_f16))], + f128: #[cfg(any(miri, target_has_reliable_f128))], + }, + test { + let one: Float = 1.0; + let zero: Float = 0.0; + assert!(Float::INFINITY.is_sign_positive()); + assert!(one.is_sign_positive()); + assert!(zero.is_sign_positive()); + assert!(!(-zero).is_sign_positive()); + assert!(!(-one).is_sign_positive()); + assert!(!Float::NEG_INFINITY.is_sign_positive()); + assert!(!(one / Float::NEG_INFINITY).is_sign_positive()); + assert!(Float::NAN.is_sign_positive()); + assert!(!(-Float::NAN).is_sign_positive()); + } +} From 71973fcbdb1c6681b603c675bb3177724ae2e403 Mon Sep 17 00:00:00 2001 From: Roger Curley Date: Sun, 3 Aug 2025 22:25:49 -0400 Subject: [PATCH 20/47] Consolidate is_sign_negative tests --- library/coretests/tests/floats/f128.rs | 13 ------------- library/coretests/tests/floats/f16.rs | 13 ------------- library/coretests/tests/floats/f32.rs | 13 ------------- library/coretests/tests/floats/f64.rs | 13 ------------- library/coretests/tests/floats/mod.rs | 21 +++++++++++++++++++++ 5 files changed, 21 insertions(+), 52 deletions(-) diff --git a/library/coretests/tests/floats/f128.rs b/library/coretests/tests/floats/f128.rs index d67cae19bb794..d3f8e8ef1d290 100644 --- a/library/coretests/tests/floats/f128.rs +++ b/library/coretests/tests/floats/f128.rs @@ -39,19 +39,6 @@ const NAN_MASK2: u128 = 0x00005555555555555555555555555555; // FIXME(f16_f128,miri): many of these have to be disabled since miri does not yet support // the intrinsics. -#[test] -fn test_is_sign_negative() { - assert!(!f128::INFINITY.is_sign_negative()); - assert!(!1f128.is_sign_negative()); - assert!(!0f128.is_sign_negative()); - assert!((-0f128).is_sign_negative()); - assert!((-1f128).is_sign_negative()); - assert!(f128::NEG_INFINITY.is_sign_negative()); - assert!((1f128 / f128::NEG_INFINITY).is_sign_negative()); - assert!(!f128::NAN.is_sign_negative()); - assert!((-f128::NAN).is_sign_negative()); -} - #[test] fn test_next_up() { let tiny = f128::from_bits(TINY_BITS); diff --git a/library/coretests/tests/floats/f16.rs b/library/coretests/tests/floats/f16.rs index 9ec43f012a2c3..db46bff1571e0 100644 --- a/library/coretests/tests/floats/f16.rs +++ b/library/coretests/tests/floats/f16.rs @@ -45,19 +45,6 @@ const NAN_MASK2: u16 = 0x0155; // FIXME(f16_f128,miri): many of these have to be disabled since miri does not yet support // the intrinsics. -#[test] -fn test_is_sign_negative() { - assert!(!f16::INFINITY.is_sign_negative()); - assert!(!1f16.is_sign_negative()); - assert!(!0f16.is_sign_negative()); - assert!((-0f16).is_sign_negative()); - assert!((-1f16).is_sign_negative()); - assert!(f16::NEG_INFINITY.is_sign_negative()); - assert!((1f16 / f16::NEG_INFINITY).is_sign_negative()); - assert!(!f16::NAN.is_sign_negative()); - assert!((-f16::NAN).is_sign_negative()); -} - #[test] fn test_next_up() { let tiny = f16::from_bits(TINY_BITS); diff --git a/library/coretests/tests/floats/f32.rs b/library/coretests/tests/floats/f32.rs index c45974382501a..6994e8134081c 100644 --- a/library/coretests/tests/floats/f32.rs +++ b/library/coretests/tests/floats/f32.rs @@ -29,19 +29,6 @@ const NAN_MASK2: u32 = 0x0055_5555; /// They serve as a way to get an idea of the real precision of floating point operations on different platforms. const APPROX_DELTA: f32 = if cfg!(miri) { 1e-4 } else { 1e-6 }; -#[test] -fn test_is_sign_negative() { - assert!(!f32::INFINITY.is_sign_negative()); - assert!(!1f32.is_sign_negative()); - assert!(!0f32.is_sign_negative()); - assert!((-0f32).is_sign_negative()); - assert!((-1f32).is_sign_negative()); - assert!(f32::NEG_INFINITY.is_sign_negative()); - assert!((1f32 / f32::NEG_INFINITY).is_sign_negative()); - assert!(!f32::NAN.is_sign_negative()); - assert!((-f32::NAN).is_sign_negative()); -} - #[test] fn test_next_up() { let tiny = f32::from_bits(TINY_BITS); diff --git a/library/coretests/tests/floats/f64.rs b/library/coretests/tests/floats/f64.rs index d20f45d872d8a..6b5fc8f69c3fb 100644 --- a/library/coretests/tests/floats/f64.rs +++ b/library/coretests/tests/floats/f64.rs @@ -24,19 +24,6 @@ const NAN_MASK1: u64 = 0x000a_aaaa_aaaa_aaaa; /// Second pattern over the mantissa const NAN_MASK2: u64 = 0x0005_5555_5555_5555; -#[test] -fn test_is_sign_negative() { - assert!(!f64::INFINITY.is_sign_negative()); - assert!(!1f64.is_sign_negative()); - assert!(!0f64.is_sign_negative()); - assert!((-0f64).is_sign_negative()); - assert!((-1f64).is_sign_negative()); - assert!(f64::NEG_INFINITY.is_sign_negative()); - assert!((1f64 / f64::NEG_INFINITY).is_sign_negative()); - assert!(!f64::NAN.is_sign_negative()); - assert!((-f64::NAN).is_sign_negative()); -} - #[test] fn test_next_up() { let tiny = f64::from_bits(TINY_BITS); diff --git a/library/coretests/tests/floats/mod.rs b/library/coretests/tests/floats/mod.rs index d45cc73174a28..b8600b30fd779 100644 --- a/library/coretests/tests/floats/mod.rs +++ b/library/coretests/tests/floats/mod.rs @@ -998,3 +998,24 @@ float_test! { assert!(!(-Float::NAN).is_sign_positive()); } } + +float_test! { + name: is_sign_negative, + attrs: { + f16: #[cfg(any(miri, target_has_reliable_f16))], + f128: #[cfg(any(miri, target_has_reliable_f128))], + }, + test { + let one: Float = 1.0; + let zero: Float = 0.0; + assert!(!Float::INFINITY.is_sign_negative()); + assert!(!one.is_sign_negative()); + assert!(!zero.is_sign_negative()); + assert!((-zero).is_sign_negative()); + assert!((-one).is_sign_negative()); + assert!(Float::NEG_INFINITY.is_sign_negative()); + assert!((one / Float::NEG_INFINITY).is_sign_negative()); + assert!(!Float::NAN.is_sign_negative()); + assert!((-Float::NAN).is_sign_negative()); + } +} From f51f68b49ae63c44f9be8b3e2a5774e177cdc5ed Mon Sep 17 00:00:00 2001 From: Roger Curley Date: Sun, 3 Aug 2025 23:08:36 -0400 Subject: [PATCH 21/47] Consolidate test_next_up Note that the behaviour of the f128 test is slightly changed to use the same nan mask as is used in test_float_bits_conv, which is the behaviour used by f16,f32,and f64. --- library/coretests/tests/floats/f128.rs | 30 ----------- library/coretests/tests/floats/f16.rs | 30 ----------- library/coretests/tests/floats/f32.rs | 30 ----------- library/coretests/tests/floats/f64.rs | 29 ----------- library/coretests/tests/floats/mod.rs | 69 ++++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 119 deletions(-) diff --git a/library/coretests/tests/floats/f128.rs b/library/coretests/tests/floats/f128.rs index d3f8e8ef1d290..71ca2abf3030e 100644 --- a/library/coretests/tests/floats/f128.rs +++ b/library/coretests/tests/floats/f128.rs @@ -39,36 +39,6 @@ const NAN_MASK2: u128 = 0x00005555555555555555555555555555; // FIXME(f16_f128,miri): many of these have to be disabled since miri does not yet support // the intrinsics. -#[test] -fn test_next_up() { - let tiny = f128::from_bits(TINY_BITS); - let tiny_up = f128::from_bits(TINY_UP_BITS); - let max_down = f128::from_bits(MAX_DOWN_BITS); - let largest_subnormal = f128::from_bits(LARGEST_SUBNORMAL_BITS); - let smallest_normal = f128::from_bits(SMALLEST_NORMAL_BITS); - assert_biteq!(f128::NEG_INFINITY.next_up(), f128::MIN); - assert_biteq!(f128::MIN.next_up(), -max_down); - assert_biteq!((-1.0 - f128::EPSILON).next_up(), -1.0f128); - assert_biteq!((-smallest_normal).next_up(), -largest_subnormal); - assert_biteq!((-tiny_up).next_up(), -tiny); - assert_biteq!((-tiny).next_up(), -0.0f128); - assert_biteq!((-0.0f128).next_up(), tiny); - assert_biteq!(0.0f128.next_up(), tiny); - assert_biteq!(tiny.next_up(), tiny_up); - assert_biteq!(largest_subnormal.next_up(), smallest_normal); - assert_biteq!(1.0f128.next_up(), 1.0 + f128::EPSILON); - assert_biteq!(f128::MAX.next_up(), f128::INFINITY); - assert_biteq!(f128::INFINITY.next_up(), f128::INFINITY); - - // Check that NaNs roundtrip. - let nan0 = f128::NAN; - let nan1 = f128::from_bits(f128::NAN.to_bits() ^ 0x002a_aaaa); - let nan2 = f128::from_bits(f128::NAN.to_bits() ^ 0x0055_5555); - assert_biteq!(nan0.next_up(), nan0); - assert_biteq!(nan1.next_up(), nan1); - assert_biteq!(nan2.next_up(), nan2); -} - #[test] fn test_next_down() { let tiny = f128::from_bits(TINY_BITS); diff --git a/library/coretests/tests/floats/f16.rs b/library/coretests/tests/floats/f16.rs index db46bff1571e0..e739e6fd0c95a 100644 --- a/library/coretests/tests/floats/f16.rs +++ b/library/coretests/tests/floats/f16.rs @@ -45,36 +45,6 @@ const NAN_MASK2: u16 = 0x0155; // FIXME(f16_f128,miri): many of these have to be disabled since miri does not yet support // the intrinsics. -#[test] -fn test_next_up() { - let tiny = f16::from_bits(TINY_BITS); - let tiny_up = f16::from_bits(TINY_UP_BITS); - let max_down = f16::from_bits(MAX_DOWN_BITS); - let largest_subnormal = f16::from_bits(LARGEST_SUBNORMAL_BITS); - let smallest_normal = f16::from_bits(SMALLEST_NORMAL_BITS); - assert_biteq!(f16::NEG_INFINITY.next_up(), f16::MIN); - assert_biteq!(f16::MIN.next_up(), -max_down); - assert_biteq!((-1.0 - f16::EPSILON).next_up(), -1.0f16); - assert_biteq!((-smallest_normal).next_up(), -largest_subnormal); - assert_biteq!((-tiny_up).next_up(), -tiny); - assert_biteq!((-tiny).next_up(), -0.0f16); - assert_biteq!((-0.0f16).next_up(), tiny); - assert_biteq!(0.0f16.next_up(), tiny); - assert_biteq!(tiny.next_up(), tiny_up); - assert_biteq!(largest_subnormal.next_up(), smallest_normal); - assert_biteq!(1.0f16.next_up(), 1.0 + f16::EPSILON); - assert_biteq!(f16::MAX.next_up(), f16::INFINITY); - assert_biteq!(f16::INFINITY.next_up(), f16::INFINITY); - - // Check that NaNs roundtrip. - let nan0 = f16::NAN; - let nan1 = f16::from_bits(f16::NAN.to_bits() ^ NAN_MASK1); - let nan2 = f16::from_bits(f16::NAN.to_bits() ^ NAN_MASK2); - assert_biteq!(nan0.next_up(), nan0); - assert_biteq!(nan1.next_up(), nan1); - assert_biteq!(nan2.next_up(), nan2); -} - #[test] fn test_next_down() { let tiny = f16::from_bits(TINY_BITS); diff --git a/library/coretests/tests/floats/f32.rs b/library/coretests/tests/floats/f32.rs index 6994e8134081c..9612660108c10 100644 --- a/library/coretests/tests/floats/f32.rs +++ b/library/coretests/tests/floats/f32.rs @@ -29,36 +29,6 @@ const NAN_MASK2: u32 = 0x0055_5555; /// They serve as a way to get an idea of the real precision of floating point operations on different platforms. const APPROX_DELTA: f32 = if cfg!(miri) { 1e-4 } else { 1e-6 }; -#[test] -fn test_next_up() { - let tiny = f32::from_bits(TINY_BITS); - let tiny_up = f32::from_bits(TINY_UP_BITS); - let max_down = f32::from_bits(MAX_DOWN_BITS); - let largest_subnormal = f32::from_bits(LARGEST_SUBNORMAL_BITS); - let smallest_normal = f32::from_bits(SMALLEST_NORMAL_BITS); - assert_biteq!(f32::NEG_INFINITY.next_up(), f32::MIN); - assert_biteq!(f32::MIN.next_up(), -max_down); - assert_biteq!((-1.0f32 - f32::EPSILON).next_up(), -1.0f32); - assert_biteq!((-smallest_normal).next_up(), -largest_subnormal); - assert_biteq!((-tiny_up).next_up(), -tiny); - assert_biteq!((-tiny).next_up(), -0.0f32); - assert_biteq!((-0.0f32).next_up(), tiny); - assert_biteq!(0.0f32.next_up(), tiny); - assert_biteq!(tiny.next_up(), tiny_up); - assert_biteq!(largest_subnormal.next_up(), smallest_normal); - assert_biteq!(1.0f32.next_up(), 1.0 + f32::EPSILON); - assert_biteq!(f32::MAX.next_up(), f32::INFINITY); - assert_biteq!(f32::INFINITY.next_up(), f32::INFINITY); - - // Check that NaNs roundtrip. - let nan0 = f32::NAN; - let nan1 = f32::from_bits(f32::NAN.to_bits() ^ NAN_MASK1); - let nan2 = f32::from_bits(f32::NAN.to_bits() ^ NAN_MASK2); - assert_biteq!(nan0.next_up(), nan0); - assert_biteq!(nan1.next_up(), nan1); - assert_biteq!(nan2.next_up(), nan2); -} - #[test] fn test_next_down() { let tiny = f32::from_bits(TINY_BITS); diff --git a/library/coretests/tests/floats/f64.rs b/library/coretests/tests/floats/f64.rs index 6b5fc8f69c3fb..cd535da681d77 100644 --- a/library/coretests/tests/floats/f64.rs +++ b/library/coretests/tests/floats/f64.rs @@ -24,35 +24,6 @@ const NAN_MASK1: u64 = 0x000a_aaaa_aaaa_aaaa; /// Second pattern over the mantissa const NAN_MASK2: u64 = 0x0005_5555_5555_5555; -#[test] -fn test_next_up() { - let tiny = f64::from_bits(TINY_BITS); - let tiny_up = f64::from_bits(TINY_UP_BITS); - let max_down = f64::from_bits(MAX_DOWN_BITS); - let largest_subnormal = f64::from_bits(LARGEST_SUBNORMAL_BITS); - let smallest_normal = f64::from_bits(SMALLEST_NORMAL_BITS); - assert_biteq!(f64::NEG_INFINITY.next_up(), f64::MIN); - assert_biteq!(f64::MIN.next_up(), -max_down); - assert_biteq!((-1.0 - f64::EPSILON).next_up(), -1.0f64); - assert_biteq!((-smallest_normal).next_up(), -largest_subnormal); - assert_biteq!((-tiny_up).next_up(), -tiny); - assert_biteq!((-tiny).next_up(), -0.0f64); - assert_biteq!((-0.0f64).next_up(), tiny); - assert_biteq!(0.0f64.next_up(), tiny); - assert_biteq!(tiny.next_up(), tiny_up); - assert_biteq!(largest_subnormal.next_up(), smallest_normal); - assert_biteq!(1.0f64.next_up(), 1.0 + f64::EPSILON); - assert_biteq!(f64::MAX.next_up(), f64::INFINITY); - assert_biteq!(f64::INFINITY.next_up(), f64::INFINITY); - - let nan0 = f64::NAN; - let nan1 = f64::from_bits(f64::NAN.to_bits() ^ NAN_MASK1); - let nan2 = f64::from_bits(f64::NAN.to_bits() ^ NAN_MASK2); - assert_biteq!(nan0.next_up(), nan0); - assert_biteq!(nan1.next_up(), nan1); - assert_biteq!(nan2.next_up(), nan2); -} - #[test] fn test_next_down() { let tiny = f64::from_bits(TINY_BITS); diff --git a/library/coretests/tests/floats/mod.rs b/library/coretests/tests/floats/mod.rs index b8600b30fd779..beb016ce4d023 100644 --- a/library/coretests/tests/floats/mod.rs +++ b/library/coretests/tests/floats/mod.rs @@ -2,34 +2,70 @@ use std::num::FpCategory as Fp; use std::ops::{Add, Div, Mul, Rem, Sub}; trait TestableFloat { + /// Unsigned int with the same size, for converting to/from bits. + type Int; /// Set the default tolerance for float comparison based on the type. const APPROX: Self; const MIN_POSITIVE_NORMAL: Self; const MAX_SUBNORMAL: Self; + /// Smallest number + const TINY: Self; + /// Next smallest number + const TINY_UP: Self; + /// Exponent = 0b11...10, Significand 0b1111..10. Min val > 0 + const MAX_DOWN: Self; + /// First pattern over the mantissa + const NAN_MASK1: Self::Int; + /// Second pattern over the mantissa + const NAN_MASK2: Self::Int; } impl TestableFloat for f16 { + type Int = u16; const APPROX: Self = 1e-3; const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE; const MAX_SUBNORMAL: Self = Self::MIN_POSITIVE.next_down(); + const TINY: Self = Self::from_bits(0x1); + const TINY_UP: Self = Self::from_bits(0x2); + const MAX_DOWN: Self = Self::from_bits(0x7bfe); + const NAN_MASK1: Self::Int = 0x02aa; + const NAN_MASK2: Self::Int = 0x0155; } impl TestableFloat for f32 { + type Int = u32; const APPROX: Self = 1e-6; const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE; const MAX_SUBNORMAL: Self = Self::MIN_POSITIVE.next_down(); + const TINY: Self = Self::from_bits(0x1); + const TINY_UP: Self = Self::from_bits(0x2); + const MAX_DOWN: Self = Self::from_bits(0x7f7f_fffe); + const NAN_MASK1: Self::Int = 0x002a_aaaa; + const NAN_MASK2: Self::Int = 0x0055_5555; } impl TestableFloat for f64 { + type Int = u64; const APPROX: Self = 1e-6; const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE; const MAX_SUBNORMAL: Self = Self::MIN_POSITIVE.next_down(); + const TINY: Self = Self::from_bits(0x1); + const TINY_UP: Self = Self::from_bits(0x2); + const MAX_DOWN: Self = Self::from_bits(0x7fef_ffff_ffff_fffe); + const NAN_MASK1: Self::Int = 0x000a_aaaa_aaaa_aaaa; + const NAN_MASK2: Self::Int = 0x0005_5555_5555_5555; } impl TestableFloat for f128 { + type Int = u128; const APPROX: Self = 1e-9; const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE; const MAX_SUBNORMAL: Self = Self::MIN_POSITIVE.next_down(); + const TINY: Self = Self::from_bits(0x1); + const TINY_UP: Self = Self::from_bits(0x2); + const MAX_DOWN: Self = Self::from_bits(0x7ffefffffffffffffffffffffffffffe); + const NAN_MASK1: Self::Int = 0x0000aaaaaaaaaaaaaaaaaaaaaaaaaaaa; + const NAN_MASK2: Self::Int = 0x00005555555555555555555555555555; } /// Determine the tolerance for values of the argument type. @@ -1019,3 +1055,36 @@ float_test! { assert!((-Float::NAN).is_sign_negative()); } } + +float_test! { + name: next_up, + attrs: { + f16: #[cfg(any(miri, target_has_reliable_f16))], + f128: #[cfg(any(miri, target_has_reliable_f128))], + }, + test { + let one: Float = 1.0; + let zero: Float = 0.0; + assert_biteq!(Float::NEG_INFINITY.next_up(), Float::MIN); + assert_biteq!(Float::MIN.next_up(), -Float::MAX_DOWN); + assert_biteq!((-one - Float::EPSILON).next_up(), -one); + assert_biteq!((-Float::MIN_POSITIVE_NORMAL).next_up(), -Float::MAX_SUBNORMAL); + assert_biteq!((-Float::TINY_UP).next_up(), -Float::TINY); + assert_biteq!((-Float::TINY).next_up(), -zero); + assert_biteq!((-zero).next_up(), Float::TINY); + assert_biteq!(zero.next_up(), Float::TINY); + assert_biteq!(Float::TINY.next_up(), Float::TINY_UP); + assert_biteq!(Float::MAX_SUBNORMAL.next_up(), Float::MIN_POSITIVE_NORMAL); + assert_biteq!(one.next_up(), 1.0 + Float::EPSILON); + assert_biteq!(Float::MAX.next_up(), Float::INFINITY); + assert_biteq!(Float::INFINITY.next_up(), Float::INFINITY); + + // Check that NaNs roundtrip. + let nan0 = Float::NAN; + let nan1 = Float::from_bits(Float::NAN.to_bits() ^ Float::NAN_MASK1); + let nan2 = Float::from_bits(Float::NAN.to_bits() ^ Float::NAN_MASK2); + assert_biteq!(nan0.next_up(), nan0); + assert_biteq!(nan1.next_up(), nan1); + assert_biteq!(nan2.next_up(), nan2); + } +} From 8a65ce4360eced99b7878e0f05363143a3ecfd53 Mon Sep 17 00:00:00 2001 From: Roger Curley Date: Mon, 4 Aug 2025 11:31:05 -0400 Subject: [PATCH 22/47] Consolidate test_next_down --- library/coretests/tests/floats/f128.rs | 46 -------------------------- library/coretests/tests/floats/f16.rs | 46 -------------------------- library/coretests/tests/floats/f32.rs | 46 -------------------------- library/coretests/tests/floats/f64.rs | 45 ------------------------- library/coretests/tests/floats/mod.rs | 34 +++++++++++++++++++ 5 files changed, 34 insertions(+), 183 deletions(-) diff --git a/library/coretests/tests/floats/f128.rs b/library/coretests/tests/floats/f128.rs index 71ca2abf3030e..551241c6c8248 100644 --- a/library/coretests/tests/floats/f128.rs +++ b/library/coretests/tests/floats/f128.rs @@ -15,21 +15,6 @@ const TOL: f128 = 1e-12; /// signs. const TOL_PRECISE: f128 = 1e-28; -/// Smallest number -const TINY_BITS: u128 = 0x1; - -/// Next smallest number -const TINY_UP_BITS: u128 = 0x2; - -/// Exponent = 0b11...10, Sifnificand 0b1111..10. Min val > 0 -const MAX_DOWN_BITS: u128 = 0x7ffefffffffffffffffffffffffffffe; - -/// Zeroed exponent, full significant -const LARGEST_SUBNORMAL_BITS: u128 = 0x0000ffffffffffffffffffffffffffff; - -/// Exponent = 0b1, zeroed significand -const SMALLEST_NORMAL_BITS: u128 = 0x00010000000000000000000000000000; - /// First pattern over the mantissa const NAN_MASK1: u128 = 0x0000aaaaaaaaaaaaaaaaaaaaaaaaaaaa; @@ -39,37 +24,6 @@ const NAN_MASK2: u128 = 0x00005555555555555555555555555555; // FIXME(f16_f128,miri): many of these have to be disabled since miri does not yet support // the intrinsics. -#[test] -fn test_next_down() { - let tiny = f128::from_bits(TINY_BITS); - let tiny_up = f128::from_bits(TINY_UP_BITS); - let max_down = f128::from_bits(MAX_DOWN_BITS); - let largest_subnormal = f128::from_bits(LARGEST_SUBNORMAL_BITS); - let smallest_normal = f128::from_bits(SMALLEST_NORMAL_BITS); - assert_biteq!(f128::NEG_INFINITY.next_down(), f128::NEG_INFINITY); - assert_biteq!(f128::MIN.next_down(), f128::NEG_INFINITY); - assert_biteq!((-max_down).next_down(), f128::MIN); - assert_biteq!((-1.0f128).next_down(), -1.0 - f128::EPSILON); - assert_biteq!((-largest_subnormal).next_down(), -smallest_normal); - assert_biteq!((-tiny).next_down(), -tiny_up); - assert_biteq!((-0.0f128).next_down(), -tiny); - assert_biteq!((0.0f128).next_down(), -tiny); - assert_biteq!(tiny.next_down(), 0.0f128); - assert_biteq!(tiny_up.next_down(), tiny); - assert_biteq!(smallest_normal.next_down(), largest_subnormal); - assert_biteq!((1.0 + f128::EPSILON).next_down(), 1.0f128); - assert_biteq!(f128::MAX.next_down(), max_down); - assert_biteq!(f128::INFINITY.next_down(), f128::MAX); - - // Check that NaNs roundtrip. - let nan0 = f128::NAN; - let nan1 = f128::from_bits(f128::NAN.to_bits() ^ 0x002a_aaaa); - let nan2 = f128::from_bits(f128::NAN.to_bits() ^ 0x0055_5555); - assert_biteq!(nan0.next_down(), nan0); - assert_biteq!(nan1.next_down(), nan1); - assert_biteq!(nan2.next_down(), nan2); -} - #[test] #[cfg(not(miri))] #[cfg(target_has_reliable_f128_math)] diff --git a/library/coretests/tests/floats/f16.rs b/library/coretests/tests/floats/f16.rs index e739e6fd0c95a..23c2c7acb6937 100644 --- a/library/coretests/tests/floats/f16.rs +++ b/library/coretests/tests/floats/f16.rs @@ -21,21 +21,6 @@ const TOL_P2: f16 = 0.5; #[allow(unused)] const TOL_P4: f16 = 10.0; -/// Smallest number -const TINY_BITS: u16 = 0x1; - -/// Next smallest number -const TINY_UP_BITS: u16 = 0x2; - -/// Exponent = 0b11...10, Sifnificand 0b1111..10. Min val > 0 -const MAX_DOWN_BITS: u16 = 0x7bfe; - -/// Zeroed exponent, full significant -const LARGEST_SUBNORMAL_BITS: u16 = 0x03ff; - -/// Exponent = 0b1, zeroed significand -const SMALLEST_NORMAL_BITS: u16 = 0x0400; - /// First pattern over the mantissa const NAN_MASK1: u16 = 0x02aa; @@ -45,37 +30,6 @@ const NAN_MASK2: u16 = 0x0155; // FIXME(f16_f128,miri): many of these have to be disabled since miri does not yet support // the intrinsics. -#[test] -fn test_next_down() { - let tiny = f16::from_bits(TINY_BITS); - let tiny_up = f16::from_bits(TINY_UP_BITS); - let max_down = f16::from_bits(MAX_DOWN_BITS); - let largest_subnormal = f16::from_bits(LARGEST_SUBNORMAL_BITS); - let smallest_normal = f16::from_bits(SMALLEST_NORMAL_BITS); - assert_biteq!(f16::NEG_INFINITY.next_down(), f16::NEG_INFINITY); - assert_biteq!(f16::MIN.next_down(), f16::NEG_INFINITY); - assert_biteq!((-max_down).next_down(), f16::MIN); - assert_biteq!((-1.0f16).next_down(), -1.0 - f16::EPSILON); - assert_biteq!((-largest_subnormal).next_down(), -smallest_normal); - assert_biteq!((-tiny).next_down(), -tiny_up); - assert_biteq!((-0.0f16).next_down(), -tiny); - assert_biteq!((0.0f16).next_down(), -tiny); - assert_biteq!(tiny.next_down(), 0.0f16); - assert_biteq!(tiny_up.next_down(), tiny); - assert_biteq!(smallest_normal.next_down(), largest_subnormal); - assert_biteq!((1.0 + f16::EPSILON).next_down(), 1.0f16); - assert_biteq!(f16::MAX.next_down(), max_down); - assert_biteq!(f16::INFINITY.next_down(), f16::MAX); - - // Check that NaNs roundtrip. - let nan0 = f16::NAN; - let nan1 = f16::from_bits(f16::NAN.to_bits() ^ NAN_MASK1); - let nan2 = f16::from_bits(f16::NAN.to_bits() ^ NAN_MASK2); - assert_biteq!(nan0.next_down(), nan0); - assert_biteq!(nan1.next_down(), nan1); - assert_biteq!(nan2.next_down(), nan2); -} - #[test] #[cfg(not(miri))] #[cfg(target_has_reliable_f16_math)] diff --git a/library/coretests/tests/floats/f32.rs b/library/coretests/tests/floats/f32.rs index 9612660108c10..552cac942f9e6 100644 --- a/library/coretests/tests/floats/f32.rs +++ b/library/coretests/tests/floats/f32.rs @@ -3,21 +3,6 @@ use core::f32::consts; use super::{assert_approx_eq, assert_biteq}; -/// Smallest number -const TINY_BITS: u32 = 0x1; - -/// Next smallest number -const TINY_UP_BITS: u32 = 0x2; - -/// Exponent = 0b11...10, Sifnificand 0b1111..10. Min val > 0 -const MAX_DOWN_BITS: u32 = 0x7f7f_fffe; - -/// Zeroed exponent, full significant -const LARGEST_SUBNORMAL_BITS: u32 = 0x007f_ffff; - -/// Exponent = 0b1, zeroed significand -const SMALLEST_NORMAL_BITS: u32 = 0x0080_0000; - /// First pattern over the mantissa const NAN_MASK1: u32 = 0x002a_aaaa; @@ -29,37 +14,6 @@ const NAN_MASK2: u32 = 0x0055_5555; /// They serve as a way to get an idea of the real precision of floating point operations on different platforms. const APPROX_DELTA: f32 = if cfg!(miri) { 1e-4 } else { 1e-6 }; -#[test] -fn test_next_down() { - let tiny = f32::from_bits(TINY_BITS); - let tiny_up = f32::from_bits(TINY_UP_BITS); - let max_down = f32::from_bits(MAX_DOWN_BITS); - let largest_subnormal = f32::from_bits(LARGEST_SUBNORMAL_BITS); - let smallest_normal = f32::from_bits(SMALLEST_NORMAL_BITS); - assert_biteq!(f32::NEG_INFINITY.next_down(), f32::NEG_INFINITY); - assert_biteq!(f32::MIN.next_down(), f32::NEG_INFINITY); - assert_biteq!((-max_down).next_down(), f32::MIN); - assert_biteq!((-1.0f32).next_down(), -1.0 - f32::EPSILON); - assert_biteq!((-largest_subnormal).next_down(), -smallest_normal); - assert_biteq!((-tiny).next_down(), -tiny_up); - assert_biteq!((-0.0f32).next_down(), -tiny); - assert_biteq!((0.0f32).next_down(), -tiny); - assert_biteq!(tiny.next_down(), 0.0f32); - assert_biteq!(tiny_up.next_down(), tiny); - assert_biteq!(smallest_normal.next_down(), largest_subnormal); - assert_biteq!((1.0 + f32::EPSILON).next_down(), 1.0f32); - assert_biteq!(f32::MAX.next_down(), max_down); - assert_biteq!(f32::INFINITY.next_down(), f32::MAX); - - // Check that NaNs roundtrip. - let nan0 = f32::NAN; - let nan1 = f32::from_bits(f32::NAN.to_bits() ^ NAN_MASK1); - let nan2 = f32::from_bits(f32::NAN.to_bits() ^ NAN_MASK2); - assert_biteq!(nan0.next_down(), nan0); - assert_biteq!(nan1.next_down(), nan1); - assert_biteq!(nan2.next_down(), nan2); -} - // FIXME(#140515): mingw has an incorrect fma https://sourceforge.net/p/mingw-w64/bugs/848/ #[cfg_attr(all(target_os = "windows", target_env = "gnu", not(target_abi = "llvm")), ignore)] #[test] diff --git a/library/coretests/tests/floats/f64.rs b/library/coretests/tests/floats/f64.rs index cd535da681d77..1c82406a91e4f 100644 --- a/library/coretests/tests/floats/f64.rs +++ b/library/coretests/tests/floats/f64.rs @@ -3,57 +3,12 @@ use core::f64::consts; use super::{assert_approx_eq, assert_biteq}; -/// Smallest number -const TINY_BITS: u64 = 0x1; - -/// Next smallest number -const TINY_UP_BITS: u64 = 0x2; - -/// Exponent = 0b11...10, Sifnificand 0b1111..10. Min val > 0 -const MAX_DOWN_BITS: u64 = 0x7fef_ffff_ffff_fffe; - -/// Zeroed exponent, full significant -const LARGEST_SUBNORMAL_BITS: u64 = 0x000f_ffff_ffff_ffff; - -/// Exponent = 0b1, zeroed significand -const SMALLEST_NORMAL_BITS: u64 = 0x0010_0000_0000_0000; - /// First pattern over the mantissa const NAN_MASK1: u64 = 0x000a_aaaa_aaaa_aaaa; /// Second pattern over the mantissa const NAN_MASK2: u64 = 0x0005_5555_5555_5555; -#[test] -fn test_next_down() { - let tiny = f64::from_bits(TINY_BITS); - let tiny_up = f64::from_bits(TINY_UP_BITS); - let max_down = f64::from_bits(MAX_DOWN_BITS); - let largest_subnormal = f64::from_bits(LARGEST_SUBNORMAL_BITS); - let smallest_normal = f64::from_bits(SMALLEST_NORMAL_BITS); - assert_biteq!(f64::NEG_INFINITY.next_down(), f64::NEG_INFINITY); - assert_biteq!(f64::MIN.next_down(), f64::NEG_INFINITY); - assert_biteq!((-max_down).next_down(), f64::MIN); - assert_biteq!((-1.0f64).next_down(), -1.0 - f64::EPSILON); - assert_biteq!((-largest_subnormal).next_down(), -smallest_normal); - assert_biteq!((-tiny).next_down(), -tiny_up); - assert_biteq!((-0.0f64).next_down(), -tiny); - assert_biteq!((0.0f64).next_down(), -tiny); - assert_biteq!(tiny.next_down(), 0.0f64); - assert_biteq!(tiny_up.next_down(), tiny); - assert_biteq!(smallest_normal.next_down(), largest_subnormal); - assert_biteq!((1.0 + f64::EPSILON).next_down(), 1.0f64); - assert_biteq!(f64::MAX.next_down(), max_down); - assert_biteq!(f64::INFINITY.next_down(), f64::MAX); - - let nan0 = f64::NAN; - let nan1 = f64::from_bits(f64::NAN.to_bits() ^ NAN_MASK1); - let nan2 = f64::from_bits(f64::NAN.to_bits() ^ NAN_MASK2); - assert_biteq!(nan0.next_down(), nan0); - assert_biteq!(nan1.next_down(), nan1); - assert_biteq!(nan2.next_down(), nan2); -} - // FIXME(#140515): mingw has an incorrect fma https://sourceforge.net/p/mingw-w64/bugs/848/ #[cfg_attr(all(target_os = "windows", target_env = "gnu", not(target_abi = "llvm")), ignore)] #[test] diff --git a/library/coretests/tests/floats/mod.rs b/library/coretests/tests/floats/mod.rs index beb016ce4d023..e9210c558f143 100644 --- a/library/coretests/tests/floats/mod.rs +++ b/library/coretests/tests/floats/mod.rs @@ -1088,3 +1088,37 @@ float_test! { assert_biteq!(nan2.next_up(), nan2); } } + +float_test! { + name: next_down, + attrs: { + f16: #[cfg(any(miri, target_has_reliable_f16))], + f128: #[cfg(any(miri, target_has_reliable_f128))], + }, + test { + let one: Float = 1.0; + let zero: Float = 0.0; + assert_biteq!(Float::NEG_INFINITY.next_down(), Float::NEG_INFINITY); + assert_biteq!(Float::MIN.next_down(), Float::NEG_INFINITY); + assert_biteq!((-Float::MAX_DOWN).next_down(), Float::MIN); + assert_biteq!((-one).next_down(), -1.0 - Float::EPSILON); + assert_biteq!((-Float::MAX_SUBNORMAL).next_down(), -Float::MIN_POSITIVE_NORMAL); + assert_biteq!((-Float::TINY).next_down(), -Float::TINY_UP); + assert_biteq!((-zero).next_down(), -Float::TINY); + assert_biteq!((zero).next_down(), -Float::TINY); + assert_biteq!(Float::TINY.next_down(), zero); + assert_biteq!(Float::TINY_UP.next_down(), Float::TINY); + assert_biteq!(Float::MIN_POSITIVE_NORMAL.next_down(), Float::MAX_SUBNORMAL); + assert_biteq!((1.0 + Float::EPSILON).next_down(), one); + assert_biteq!(Float::MAX.next_down(), Float::MAX_DOWN); + assert_biteq!(Float::INFINITY.next_down(), Float::MAX); + + // Check that NaNs roundtrip. + let nan0 = Float::NAN; + let nan1 = Float::from_bits(Float::NAN.to_bits() ^ Float::NAN_MASK1); + let nan2 = Float::from_bits(Float::NAN.to_bits() ^ Float::NAN_MASK2); + assert_biteq!(nan0.next_down(), nan0); + assert_biteq!(nan1.next_down(), nan1); + assert_biteq!(nan2.next_down(), nan2); + } +} From 666bfcae214eccdef07b64671df8609a99a216eb Mon Sep 17 00:00:00 2001 From: Roger Curley Date: Mon, 4 Aug 2025 13:27:47 -0400 Subject: [PATCH 23/47] Consolidate sqrt_domain tests --- library/coretests/tests/floats/f128.rs | 13 ------------- library/coretests/tests/floats/f16.rs | 13 ------------- library/coretests/tests/floats/f32.rs | 11 ----------- library/coretests/tests/floats/f64.rs | 11 ----------- library/coretests/tests/floats/mod.rs | 23 +++++++++++++++++++++++ 5 files changed, 23 insertions(+), 48 deletions(-) diff --git a/library/coretests/tests/floats/f128.rs b/library/coretests/tests/floats/f128.rs index 551241c6c8248..43882ad89a2d6 100644 --- a/library/coretests/tests/floats/f128.rs +++ b/library/coretests/tests/floats/f128.rs @@ -78,19 +78,6 @@ fn test_powi() { assert_biteq!(neg_inf.powi(2), inf); } -#[test] -#[cfg(not(miri))] -#[cfg(target_has_reliable_f128_math)] -fn test_sqrt_domain() { - assert!(f128::NAN.sqrt().is_nan()); - assert!(f128::NEG_INFINITY.sqrt().is_nan()); - assert!((-1.0f128).sqrt().is_nan()); - assert_biteq!((-0.0f128).sqrt(), -0.0); - assert_biteq!(0.0f128.sqrt(), 0.0); - assert_biteq!(1.0f128.sqrt(), 1.0); - assert_biteq!(f128::INFINITY.sqrt(), f128::INFINITY); -} - #[test] fn test_to_degrees() { let pi: f128 = consts::PI; diff --git a/library/coretests/tests/floats/f16.rs b/library/coretests/tests/floats/f16.rs index 23c2c7acb6937..c405e4e5c85a2 100644 --- a/library/coretests/tests/floats/f16.rs +++ b/library/coretests/tests/floats/f16.rs @@ -80,19 +80,6 @@ fn test_powi() { assert_biteq!(neg_inf.powi(2), inf); } -#[test] -#[cfg(not(miri))] -#[cfg(target_has_reliable_f16_math)] -fn test_sqrt_domain() { - assert!(f16::NAN.sqrt().is_nan()); - assert!(f16::NEG_INFINITY.sqrt().is_nan()); - assert!((-1.0f16).sqrt().is_nan()); - assert_biteq!((-0.0f16).sqrt(), -0.0); - assert_biteq!(0.0f16.sqrt(), 0.0); - assert_biteq!(1.0f16.sqrt(), 1.0); - assert_biteq!(f16::INFINITY.sqrt(), f16::INFINITY); -} - #[test] fn test_to_degrees() { let pi: f16 = consts::PI; diff --git a/library/coretests/tests/floats/f32.rs b/library/coretests/tests/floats/f32.rs index 552cac942f9e6..f08b033fb5ea9 100644 --- a/library/coretests/tests/floats/f32.rs +++ b/library/coretests/tests/floats/f32.rs @@ -60,17 +60,6 @@ fn test_powi() { assert_biteq!(neg_inf.powi(2), inf); } -#[test] -fn test_sqrt_domain() { - assert!(f32::NAN.sqrt().is_nan()); - assert!(f32::NEG_INFINITY.sqrt().is_nan()); - assert!((-1.0f32).sqrt().is_nan()); - assert_biteq!((-0.0f32).sqrt(), -0.0); - assert_biteq!(0.0f32.sqrt(), 0.0); - assert_biteq!(1.0f32.sqrt(), 1.0); - assert_biteq!(f32::INFINITY.sqrt(), f32::INFINITY); -} - #[test] fn test_to_degrees() { let pi: f32 = consts::PI; diff --git a/library/coretests/tests/floats/f64.rs b/library/coretests/tests/floats/f64.rs index 1c82406a91e4f..8d7d31b57c18f 100644 --- a/library/coretests/tests/floats/f64.rs +++ b/library/coretests/tests/floats/f64.rs @@ -55,17 +55,6 @@ fn test_powi() { assert_biteq!(neg_inf.powi(2), inf); } -#[test] -fn test_sqrt_domain() { - assert!(f64::NAN.sqrt().is_nan()); - assert!(f64::NEG_INFINITY.sqrt().is_nan()); - assert!((-1.0f64).sqrt().is_nan()); - assert_biteq!((-0.0f64).sqrt(), -0.0); - assert_biteq!(0.0f64.sqrt(), 0.0); - assert_biteq!(1.0f64.sqrt(), 1.0); - assert_biteq!(f64::INFINITY.sqrt(), f64::INFINITY); -} - #[test] fn test_to_degrees() { let pi: f64 = consts::PI; diff --git a/library/coretests/tests/floats/mod.rs b/library/coretests/tests/floats/mod.rs index e9210c558f143..68ed8d47094f6 100644 --- a/library/coretests/tests/floats/mod.rs +++ b/library/coretests/tests/floats/mod.rs @@ -1122,3 +1122,26 @@ float_test! { assert_biteq!(nan2.next_down(), nan2); } } + +// FIXME(f16_f128,miri): many of these have to be disabled since miri does not yet support +// the intrinsics. + +float_test! { + name: sqrt_domain, + attrs: { + const: #[cfg(false)], + f16: #[cfg(all(not(miri), target_has_reliable_f16_math))], + f128: #[cfg(all(not(miri), target_has_reliable_f128_math))], + }, + test { + let one: Float = 1.0; + let zero: Float = 0.0; + assert!(Float::NAN.sqrt().is_nan()); + assert!(Float::NEG_INFINITY.sqrt().is_nan()); + assert!((-one).sqrt().is_nan()); + assert_biteq!((-zero).sqrt(), -zero); + assert_biteq!(zero.sqrt(), zero); + assert_biteq!(one.sqrt(), one); + assert_biteq!(Float::INFINITY.sqrt(), Float::INFINITY); + } +} From a5e4e7ab52a031153ae7cef169ea7fa50cc6ae1c Mon Sep 17 00:00:00 2001 From: Roger Curley Date: Mon, 4 Aug 2025 13:59:47 -0400 Subject: [PATCH 24/47] Consolidate clamp tests --- library/coretests/tests/floats/f128.rs | 18 ----------- library/coretests/tests/floats/f16.rs | 18 ----------- library/coretests/tests/floats/f32.rs | 18 ----------- library/coretests/tests/floats/f64.rs | 18 ----------- library/coretests/tests/floats/mod.rs | 45 ++++++++++++++++++++++++++ 5 files changed, 45 insertions(+), 72 deletions(-) diff --git a/library/coretests/tests/floats/f128.rs b/library/coretests/tests/floats/f128.rs index 43882ad89a2d6..0ac297d505236 100644 --- a/library/coretests/tests/floats/f128.rs +++ b/library/coretests/tests/floats/f128.rs @@ -132,24 +132,6 @@ fn test_float_bits_conv() { assert_eq!(f128::from_bits(masked_nan2).to_bits(), masked_nan2); } -#[test] -#[should_panic] -fn test_clamp_min_greater_than_max() { - let _ = 1.0f128.clamp(3.0, 1.0); -} - -#[test] -#[should_panic] -fn test_clamp_min_is_nan() { - let _ = 1.0f128.clamp(f128::NAN, 1.0); -} - -#[test] -#[should_panic] -fn test_clamp_max_is_nan() { - let _ = 1.0f128.clamp(3.0, f128::NAN); -} - #[test] fn test_total_cmp() { use core::cmp::Ordering; diff --git a/library/coretests/tests/floats/f16.rs b/library/coretests/tests/floats/f16.rs index c405e4e5c85a2..4088ed79aaf35 100644 --- a/library/coretests/tests/floats/f16.rs +++ b/library/coretests/tests/floats/f16.rs @@ -131,24 +131,6 @@ fn test_float_bits_conv() { assert_eq!(f16::from_bits(masked_nan2).to_bits(), masked_nan2); } -#[test] -#[should_panic] -fn test_clamp_min_greater_than_max() { - let _ = 1.0f16.clamp(3.0, 1.0); -} - -#[test] -#[should_panic] -fn test_clamp_min_is_nan() { - let _ = 1.0f16.clamp(f16::NAN, 1.0); -} - -#[test] -#[should_panic] -fn test_clamp_max_is_nan() { - let _ = 1.0f16.clamp(3.0, f16::NAN); -} - #[test] #[cfg(not(miri))] #[cfg(target_has_reliable_f16_math)] diff --git a/library/coretests/tests/floats/f32.rs b/library/coretests/tests/floats/f32.rs index f08b033fb5ea9..20d04fcf35aff 100644 --- a/library/coretests/tests/floats/f32.rs +++ b/library/coretests/tests/floats/f32.rs @@ -112,24 +112,6 @@ fn test_float_bits_conv() { assert_eq!(f32::from_bits(masked_nan2).to_bits(), masked_nan2); } -#[test] -#[should_panic] -fn test_clamp_min_greater_than_max() { - let _ = 1.0f32.clamp(3.0, 1.0); -} - -#[test] -#[should_panic] -fn test_clamp_min_is_nan() { - let _ = 1.0f32.clamp(f32::NAN, 1.0); -} - -#[test] -#[should_panic] -fn test_clamp_max_is_nan() { - let _ = 1.0f32.clamp(3.0, f32::NAN); -} - #[test] fn test_total_cmp() { use core::cmp::Ordering; diff --git a/library/coretests/tests/floats/f64.rs b/library/coretests/tests/floats/f64.rs index 8d7d31b57c18f..9d97a61e762c3 100644 --- a/library/coretests/tests/floats/f64.rs +++ b/library/coretests/tests/floats/f64.rs @@ -105,24 +105,6 @@ fn test_float_bits_conv() { assert_eq!(f64::from_bits(masked_nan2).to_bits(), masked_nan2); } -#[test] -#[should_panic] -fn test_clamp_min_greater_than_max() { - let _ = 1.0f64.clamp(3.0, 1.0); -} - -#[test] -#[should_panic] -fn test_clamp_min_is_nan() { - let _ = 1.0f64.clamp(f64::NAN, 1.0); -} - -#[test] -#[should_panic] -fn test_clamp_max_is_nan() { - let _ = 1.0f64.clamp(3.0, f64::NAN); -} - #[test] fn test_total_cmp() { use core::cmp::Ordering; diff --git a/library/coretests/tests/floats/mod.rs b/library/coretests/tests/floats/mod.rs index 68ed8d47094f6..ad25b49d8a313 100644 --- a/library/coretests/tests/floats/mod.rs +++ b/library/coretests/tests/floats/mod.rs @@ -1145,3 +1145,48 @@ float_test! { assert_biteq!(Float::INFINITY.sqrt(), Float::INFINITY); } } + +float_test! { + name: clamp_min_greater_than_max, + attrs: { + const: #[cfg(false)], + f16: #[should_panic, cfg(any(miri, target_has_reliable_f16))], + f32: #[should_panic], + f64: #[should_panic], + f128: #[should_panic, cfg(any(miri, target_has_reliable_f128))], + }, + test { + let one : Float = 1.0; + let _ = one.clamp(3.0, 1.0); + } +} + +float_test! { + name: clamp_min_is_nan, + attrs: { + const: #[cfg(false)], + f16: #[should_panic, cfg(any(miri, target_has_reliable_f16))], + f32: #[should_panic], + f64: #[should_panic], + f128: #[should_panic, cfg(any(miri, target_has_reliable_f128))], + }, + test { + let one : Float = 1.0; + let _ = one.clamp(Float::NAN, 1.0); + } +} + +float_test! { + name: clamp_max_is_nan, + attrs: { + const: #[cfg(false)], + f16: #[should_panic, cfg(any(miri, target_has_reliable_f16))], + f32: #[should_panic], + f64: #[should_panic], + f128: #[should_panic, cfg(any(miri, target_has_reliable_f128))], + }, + test { + let one : Float = 1.0; + let _ = one.clamp(3.0, Float::NAN); + } +} From 80bcd1a61f4132f47978978c648dc964f679749c Mon Sep 17 00:00:00 2001 From: Roger Curley Date: Mon, 4 Aug 2025 15:06:47 -0400 Subject: [PATCH 25/47] Consolidate total_cmp tests This standardizes how max and min subnormals are generated. Since the new method doesn't use powf, it also enables some of the tests for f128 that were previously disabled due to issues with powf (although it looks like those issues were already fixed anyway). f16 signalling nan tests previously disabled are not re-enabled, since the underlying LLVM issue has not been closed. --- library/coretests/tests/floats/f128.rs | 144 --------------------- library/coretests/tests/floats/f16.rs | 148 ---------------------- library/coretests/tests/floats/f32.rs | 143 --------------------- library/coretests/tests/floats/f64.rs | 143 --------------------- library/coretests/tests/floats/mod.rs | 166 +++++++++++++++++++++++++ 5 files changed, 166 insertions(+), 578 deletions(-) diff --git a/library/coretests/tests/floats/f128.rs b/library/coretests/tests/floats/f128.rs index 0ac297d505236..ac4a20665305c 100644 --- a/library/coretests/tests/floats/f128.rs +++ b/library/coretests/tests/floats/f128.rs @@ -132,150 +132,6 @@ fn test_float_bits_conv() { assert_eq!(f128::from_bits(masked_nan2).to_bits(), masked_nan2); } -#[test] -fn test_total_cmp() { - use core::cmp::Ordering; - - fn quiet_bit_mask() -> u128 { - 1 << (f128::MANTISSA_DIGITS - 2) - } - - // FIXME(f16_f128): test subnormals when powf is available - // fn min_subnorm() -> f128 { - // f128::MIN_POSITIVE / f128::powf(2.0, f128::MANTISSA_DIGITS as f128 - 1.0) - // } - - // fn max_subnorm() -> f128 { - // f128::MIN_POSITIVE - min_subnorm() - // } - - fn q_nan() -> f128 { - f128::from_bits(f128::NAN.to_bits() | quiet_bit_mask()) - } - - fn s_nan() -> f128 { - f128::from_bits((f128::NAN.to_bits() & !quiet_bit_mask()) + 42) - } - - assert_eq!(Ordering::Equal, (-q_nan()).total_cmp(&-q_nan())); - assert_eq!(Ordering::Equal, (-s_nan()).total_cmp(&-s_nan())); - assert_eq!(Ordering::Equal, (-f128::INFINITY).total_cmp(&-f128::INFINITY)); - assert_eq!(Ordering::Equal, (-f128::MAX).total_cmp(&-f128::MAX)); - assert_eq!(Ordering::Equal, (-2.5_f128).total_cmp(&-2.5)); - assert_eq!(Ordering::Equal, (-1.0_f128).total_cmp(&-1.0)); - assert_eq!(Ordering::Equal, (-1.5_f128).total_cmp(&-1.5)); - assert_eq!(Ordering::Equal, (-0.5_f128).total_cmp(&-0.5)); - assert_eq!(Ordering::Equal, (-f128::MIN_POSITIVE).total_cmp(&-f128::MIN_POSITIVE)); - // assert_eq!(Ordering::Equal, (-max_subnorm()).total_cmp(&-max_subnorm())); - // assert_eq!(Ordering::Equal, (-min_subnorm()).total_cmp(&-min_subnorm())); - assert_eq!(Ordering::Equal, (-0.0_f128).total_cmp(&-0.0)); - assert_eq!(Ordering::Equal, 0.0_f128.total_cmp(&0.0)); - // assert_eq!(Ordering::Equal, min_subnorm().total_cmp(&min_subnorm())); - // assert_eq!(Ordering::Equal, max_subnorm().total_cmp(&max_subnorm())); - assert_eq!(Ordering::Equal, f128::MIN_POSITIVE.total_cmp(&f128::MIN_POSITIVE)); - assert_eq!(Ordering::Equal, 0.5_f128.total_cmp(&0.5)); - assert_eq!(Ordering::Equal, 1.0_f128.total_cmp(&1.0)); - assert_eq!(Ordering::Equal, 1.5_f128.total_cmp(&1.5)); - assert_eq!(Ordering::Equal, 2.5_f128.total_cmp(&2.5)); - assert_eq!(Ordering::Equal, f128::MAX.total_cmp(&f128::MAX)); - assert_eq!(Ordering::Equal, f128::INFINITY.total_cmp(&f128::INFINITY)); - assert_eq!(Ordering::Equal, s_nan().total_cmp(&s_nan())); - assert_eq!(Ordering::Equal, q_nan().total_cmp(&q_nan())); - - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-s_nan())); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f128::INFINITY)); - assert_eq!(Ordering::Less, (-f128::INFINITY).total_cmp(&-f128::MAX)); - assert_eq!(Ordering::Less, (-f128::MAX).total_cmp(&-2.5)); - assert_eq!(Ordering::Less, (-2.5_f128).total_cmp(&-1.5)); - assert_eq!(Ordering::Less, (-1.5_f128).total_cmp(&-1.0)); - assert_eq!(Ordering::Less, (-1.0_f128).total_cmp(&-0.5)); - assert_eq!(Ordering::Less, (-0.5_f128).total_cmp(&-f128::MIN_POSITIVE)); - // assert_eq!(Ordering::Less, (-f128::MIN_POSITIVE).total_cmp(&-max_subnorm())); - // assert_eq!(Ordering::Less, (-max_subnorm()).total_cmp(&-min_subnorm())); - // assert_eq!(Ordering::Less, (-min_subnorm()).total_cmp(&-0.0)); - assert_eq!(Ordering::Less, (-0.0_f128).total_cmp(&0.0)); - // assert_eq!(Ordering::Less, 0.0_f128.total_cmp(&min_subnorm())); - // assert_eq!(Ordering::Less, min_subnorm().total_cmp(&max_subnorm())); - // assert_eq!(Ordering::Less, max_subnorm().total_cmp(&f128::MIN_POSITIVE)); - assert_eq!(Ordering::Less, f128::MIN_POSITIVE.total_cmp(&0.5)); - assert_eq!(Ordering::Less, 0.5_f128.total_cmp(&1.0)); - assert_eq!(Ordering::Less, 1.0_f128.total_cmp(&1.5)); - assert_eq!(Ordering::Less, 1.5_f128.total_cmp(&2.5)); - assert_eq!(Ordering::Less, 2.5_f128.total_cmp(&f128::MAX)); - assert_eq!(Ordering::Less, f128::MAX.total_cmp(&f128::INFINITY)); - assert_eq!(Ordering::Less, f128::INFINITY.total_cmp(&s_nan())); - assert_eq!(Ordering::Less, s_nan().total_cmp(&q_nan())); - - assert_eq!(Ordering::Greater, (-s_nan()).total_cmp(&-q_nan())); - assert_eq!(Ordering::Greater, (-f128::INFINITY).total_cmp(&-s_nan())); - assert_eq!(Ordering::Greater, (-f128::MAX).total_cmp(&-f128::INFINITY)); - assert_eq!(Ordering::Greater, (-2.5_f128).total_cmp(&-f128::MAX)); - assert_eq!(Ordering::Greater, (-1.5_f128).total_cmp(&-2.5)); - assert_eq!(Ordering::Greater, (-1.0_f128).total_cmp(&-1.5)); - assert_eq!(Ordering::Greater, (-0.5_f128).total_cmp(&-1.0)); - assert_eq!(Ordering::Greater, (-f128::MIN_POSITIVE).total_cmp(&-0.5)); - // assert_eq!(Ordering::Greater, (-max_subnorm()).total_cmp(&-f128::MIN_POSITIVE)); - // assert_eq!(Ordering::Greater, (-min_subnorm()).total_cmp(&-max_subnorm())); - // assert_eq!(Ordering::Greater, (-0.0_f128).total_cmp(&-min_subnorm())); - assert_eq!(Ordering::Greater, 0.0_f128.total_cmp(&-0.0)); - // assert_eq!(Ordering::Greater, min_subnorm().total_cmp(&0.0)); - // assert_eq!(Ordering::Greater, max_subnorm().total_cmp(&min_subnorm())); - // assert_eq!(Ordering::Greater, f128::MIN_POSITIVE.total_cmp(&max_subnorm())); - assert_eq!(Ordering::Greater, 0.5_f128.total_cmp(&f128::MIN_POSITIVE)); - assert_eq!(Ordering::Greater, 1.0_f128.total_cmp(&0.5)); - assert_eq!(Ordering::Greater, 1.5_f128.total_cmp(&1.0)); - assert_eq!(Ordering::Greater, 2.5_f128.total_cmp(&1.5)); - assert_eq!(Ordering::Greater, f128::MAX.total_cmp(&2.5)); - assert_eq!(Ordering::Greater, f128::INFINITY.total_cmp(&f128::MAX)); - assert_eq!(Ordering::Greater, s_nan().total_cmp(&f128::INFINITY)); - assert_eq!(Ordering::Greater, q_nan().total_cmp(&s_nan())); - - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-s_nan())); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-f128::INFINITY)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-f128::MAX)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-2.5)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-1.5)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-1.0)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-0.5)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-f128::MIN_POSITIVE)); - // assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-max_subnorm())); - // assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-min_subnorm())); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-0.0)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&0.0)); - // assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&min_subnorm())); - // assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&max_subnorm())); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&f128::MIN_POSITIVE)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&0.5)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&1.0)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&1.5)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&2.5)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&f128::MAX)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&f128::INFINITY)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&s_nan())); - - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f128::INFINITY)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f128::MAX)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-2.5)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-1.5)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-1.0)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-0.5)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f128::MIN_POSITIVE)); - // assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-max_subnorm())); - // assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-min_subnorm())); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-0.0)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&0.0)); - // assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&min_subnorm())); - // assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&max_subnorm())); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&f128::MIN_POSITIVE)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&0.5)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&1.0)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&1.5)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&2.5)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&f128::MAX)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&f128::INFINITY)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&s_nan())); -} - #[test] fn test_algebraic() { let a: f128 = 123.0; diff --git a/library/coretests/tests/floats/f16.rs b/library/coretests/tests/floats/f16.rs index 4088ed79aaf35..bb9c8a002fe88 100644 --- a/library/coretests/tests/floats/f16.rs +++ b/library/coretests/tests/floats/f16.rs @@ -131,154 +131,6 @@ fn test_float_bits_conv() { assert_eq!(f16::from_bits(masked_nan2).to_bits(), masked_nan2); } -#[test] -#[cfg(not(miri))] -#[cfg(target_has_reliable_f16_math)] -fn test_total_cmp() { - use core::cmp::Ordering; - - fn quiet_bit_mask() -> u16 { - 1 << (f16::MANTISSA_DIGITS - 2) - } - - fn min_subnorm() -> f16 { - f16::MIN_POSITIVE / f16::powf(2.0, f16::MANTISSA_DIGITS as f16 - 1.0) - } - - fn max_subnorm() -> f16 { - f16::MIN_POSITIVE - min_subnorm() - } - - fn q_nan() -> f16 { - f16::from_bits(f16::NAN.to_bits() | quiet_bit_mask()) - } - - // FIXME(f16_f128): Tests involving sNaN are disabled because without optimizations, - // `total_cmp` is getting incorrectly lowered to code that includes a `extend`/`trunc` round - // trip, which quiets sNaNs. See: https://github.com/llvm/llvm-project/issues/104915 - // fn s_nan() -> f16 { - // f16::from_bits((f16::NAN.to_bits() & !quiet_bit_mask()) + 42) - // } - - assert_eq!(Ordering::Equal, (-q_nan()).total_cmp(&-q_nan())); - // assert_eq!(Ordering::Equal, (-s_nan()).total_cmp(&-s_nan())); - assert_eq!(Ordering::Equal, (-f16::INFINITY).total_cmp(&-f16::INFINITY)); - assert_eq!(Ordering::Equal, (-f16::MAX).total_cmp(&-f16::MAX)); - assert_eq!(Ordering::Equal, (-2.5_f16).total_cmp(&-2.5)); - assert_eq!(Ordering::Equal, (-1.0_f16).total_cmp(&-1.0)); - assert_eq!(Ordering::Equal, (-1.5_f16).total_cmp(&-1.5)); - assert_eq!(Ordering::Equal, (-0.5_f16).total_cmp(&-0.5)); - assert_eq!(Ordering::Equal, (-f16::MIN_POSITIVE).total_cmp(&-f16::MIN_POSITIVE)); - assert_eq!(Ordering::Equal, (-max_subnorm()).total_cmp(&-max_subnorm())); - assert_eq!(Ordering::Equal, (-min_subnorm()).total_cmp(&-min_subnorm())); - assert_eq!(Ordering::Equal, (-0.0_f16).total_cmp(&-0.0)); - assert_eq!(Ordering::Equal, 0.0_f16.total_cmp(&0.0)); - assert_eq!(Ordering::Equal, min_subnorm().total_cmp(&min_subnorm())); - assert_eq!(Ordering::Equal, max_subnorm().total_cmp(&max_subnorm())); - assert_eq!(Ordering::Equal, f16::MIN_POSITIVE.total_cmp(&f16::MIN_POSITIVE)); - assert_eq!(Ordering::Equal, 0.5_f16.total_cmp(&0.5)); - assert_eq!(Ordering::Equal, 1.0_f16.total_cmp(&1.0)); - assert_eq!(Ordering::Equal, 1.5_f16.total_cmp(&1.5)); - assert_eq!(Ordering::Equal, 2.5_f16.total_cmp(&2.5)); - assert_eq!(Ordering::Equal, f16::MAX.total_cmp(&f16::MAX)); - assert_eq!(Ordering::Equal, f16::INFINITY.total_cmp(&f16::INFINITY)); - // assert_eq!(Ordering::Equal, s_nan().total_cmp(&s_nan())); - assert_eq!(Ordering::Equal, q_nan().total_cmp(&q_nan())); - - // assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-s_nan())); - // assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f16::INFINITY)); - assert_eq!(Ordering::Less, (-f16::INFINITY).total_cmp(&-f16::MAX)); - assert_eq!(Ordering::Less, (-f16::MAX).total_cmp(&-2.5)); - assert_eq!(Ordering::Less, (-2.5_f16).total_cmp(&-1.5)); - assert_eq!(Ordering::Less, (-1.5_f16).total_cmp(&-1.0)); - assert_eq!(Ordering::Less, (-1.0_f16).total_cmp(&-0.5)); - assert_eq!(Ordering::Less, (-0.5_f16).total_cmp(&-f16::MIN_POSITIVE)); - assert_eq!(Ordering::Less, (-f16::MIN_POSITIVE).total_cmp(&-max_subnorm())); - assert_eq!(Ordering::Less, (-max_subnorm()).total_cmp(&-min_subnorm())); - assert_eq!(Ordering::Less, (-min_subnorm()).total_cmp(&-0.0)); - assert_eq!(Ordering::Less, (-0.0_f16).total_cmp(&0.0)); - assert_eq!(Ordering::Less, 0.0_f16.total_cmp(&min_subnorm())); - assert_eq!(Ordering::Less, min_subnorm().total_cmp(&max_subnorm())); - assert_eq!(Ordering::Less, max_subnorm().total_cmp(&f16::MIN_POSITIVE)); - assert_eq!(Ordering::Less, f16::MIN_POSITIVE.total_cmp(&0.5)); - assert_eq!(Ordering::Less, 0.5_f16.total_cmp(&1.0)); - assert_eq!(Ordering::Less, 1.0_f16.total_cmp(&1.5)); - assert_eq!(Ordering::Less, 1.5_f16.total_cmp(&2.5)); - assert_eq!(Ordering::Less, 2.5_f16.total_cmp(&f16::MAX)); - assert_eq!(Ordering::Less, f16::MAX.total_cmp(&f16::INFINITY)); - // assert_eq!(Ordering::Less, f16::INFINITY.total_cmp(&s_nan())); - // assert_eq!(Ordering::Less, s_nan().total_cmp(&q_nan())); - - // assert_eq!(Ordering::Greater, (-s_nan()).total_cmp(&-q_nan())); - // assert_eq!(Ordering::Greater, (-f16::INFINITY).total_cmp(&-s_nan())); - assert_eq!(Ordering::Greater, (-f16::MAX).total_cmp(&-f16::INFINITY)); - assert_eq!(Ordering::Greater, (-2.5_f16).total_cmp(&-f16::MAX)); - assert_eq!(Ordering::Greater, (-1.5_f16).total_cmp(&-2.5)); - assert_eq!(Ordering::Greater, (-1.0_f16).total_cmp(&-1.5)); - assert_eq!(Ordering::Greater, (-0.5_f16).total_cmp(&-1.0)); - assert_eq!(Ordering::Greater, (-f16::MIN_POSITIVE).total_cmp(&-0.5)); - assert_eq!(Ordering::Greater, (-max_subnorm()).total_cmp(&-f16::MIN_POSITIVE)); - assert_eq!(Ordering::Greater, (-min_subnorm()).total_cmp(&-max_subnorm())); - assert_eq!(Ordering::Greater, (-0.0_f16).total_cmp(&-min_subnorm())); - assert_eq!(Ordering::Greater, 0.0_f16.total_cmp(&-0.0)); - assert_eq!(Ordering::Greater, min_subnorm().total_cmp(&0.0)); - assert_eq!(Ordering::Greater, max_subnorm().total_cmp(&min_subnorm())); - assert_eq!(Ordering::Greater, f16::MIN_POSITIVE.total_cmp(&max_subnorm())); - assert_eq!(Ordering::Greater, 0.5_f16.total_cmp(&f16::MIN_POSITIVE)); - assert_eq!(Ordering::Greater, 1.0_f16.total_cmp(&0.5)); - assert_eq!(Ordering::Greater, 1.5_f16.total_cmp(&1.0)); - assert_eq!(Ordering::Greater, 2.5_f16.total_cmp(&1.5)); - assert_eq!(Ordering::Greater, f16::MAX.total_cmp(&2.5)); - assert_eq!(Ordering::Greater, f16::INFINITY.total_cmp(&f16::MAX)); - // assert_eq!(Ordering::Greater, s_nan().total_cmp(&f16::INFINITY)); - // assert_eq!(Ordering::Greater, q_nan().total_cmp(&s_nan())); - - // assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-s_nan())); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-f16::INFINITY)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-f16::MAX)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-2.5)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-1.5)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-1.0)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-0.5)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-f16::MIN_POSITIVE)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-max_subnorm())); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-min_subnorm())); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-0.0)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&0.0)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&min_subnorm())); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&max_subnorm())); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&f16::MIN_POSITIVE)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&0.5)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&1.0)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&1.5)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&2.5)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&f16::MAX)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&f16::INFINITY)); - // assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&s_nan())); - - // assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f16::INFINITY)); - // assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f16::MAX)); - // assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-2.5)); - // assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-1.5)); - // assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-1.0)); - // assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-0.5)); - // assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f16::MIN_POSITIVE)); - // assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-max_subnorm())); - // assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-min_subnorm())); - // assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-0.0)); - // assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&0.0)); - // assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&min_subnorm())); - // assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&max_subnorm())); - // assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&f16::MIN_POSITIVE)); - // assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&0.5)); - // assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&1.0)); - // assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&1.5)); - // assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&2.5)); - // assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&f16::MAX)); - // assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&f16::INFINITY)); - // assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&s_nan())); -} - #[test] fn test_algebraic() { let a: f16 = 123.0; diff --git a/library/coretests/tests/floats/f32.rs b/library/coretests/tests/floats/f32.rs index 20d04fcf35aff..e77e44655dc88 100644 --- a/library/coretests/tests/floats/f32.rs +++ b/library/coretests/tests/floats/f32.rs @@ -112,149 +112,6 @@ fn test_float_bits_conv() { assert_eq!(f32::from_bits(masked_nan2).to_bits(), masked_nan2); } -#[test] -fn test_total_cmp() { - use core::cmp::Ordering; - - fn quiet_bit_mask() -> u32 { - 1 << (f32::MANTISSA_DIGITS - 2) - } - - fn min_subnorm() -> f32 { - f32::MIN_POSITIVE / f32::powf(2.0, f32::MANTISSA_DIGITS as f32 - 1.0) - } - - fn max_subnorm() -> f32 { - f32::MIN_POSITIVE - min_subnorm() - } - - fn q_nan() -> f32 { - f32::from_bits(f32::NAN.to_bits() | quiet_bit_mask()) - } - - fn s_nan() -> f32 { - f32::from_bits((f32::NAN.to_bits() & !quiet_bit_mask()) + 42) - } - - assert_eq!(Ordering::Equal, (-q_nan()).total_cmp(&-q_nan())); - assert_eq!(Ordering::Equal, (-s_nan()).total_cmp(&-s_nan())); - assert_eq!(Ordering::Equal, (-f32::INFINITY).total_cmp(&-f32::INFINITY)); - assert_eq!(Ordering::Equal, (-f32::MAX).total_cmp(&-f32::MAX)); - assert_eq!(Ordering::Equal, (-2.5_f32).total_cmp(&-2.5)); - assert_eq!(Ordering::Equal, (-1.0_f32).total_cmp(&-1.0)); - assert_eq!(Ordering::Equal, (-1.5_f32).total_cmp(&-1.5)); - assert_eq!(Ordering::Equal, (-0.5_f32).total_cmp(&-0.5)); - assert_eq!(Ordering::Equal, (-f32::MIN_POSITIVE).total_cmp(&-f32::MIN_POSITIVE)); - assert_eq!(Ordering::Equal, (-max_subnorm()).total_cmp(&-max_subnorm())); - assert_eq!(Ordering::Equal, (-min_subnorm()).total_cmp(&-min_subnorm())); - assert_eq!(Ordering::Equal, (-0.0_f32).total_cmp(&-0.0)); - assert_eq!(Ordering::Equal, 0.0_f32.total_cmp(&0.0)); - assert_eq!(Ordering::Equal, min_subnorm().total_cmp(&min_subnorm())); - assert_eq!(Ordering::Equal, max_subnorm().total_cmp(&max_subnorm())); - assert_eq!(Ordering::Equal, f32::MIN_POSITIVE.total_cmp(&f32::MIN_POSITIVE)); - assert_eq!(Ordering::Equal, 0.5_f32.total_cmp(&0.5)); - assert_eq!(Ordering::Equal, 1.0_f32.total_cmp(&1.0)); - assert_eq!(Ordering::Equal, 1.5_f32.total_cmp(&1.5)); - assert_eq!(Ordering::Equal, 2.5_f32.total_cmp(&2.5)); - assert_eq!(Ordering::Equal, f32::MAX.total_cmp(&f32::MAX)); - assert_eq!(Ordering::Equal, f32::INFINITY.total_cmp(&f32::INFINITY)); - assert_eq!(Ordering::Equal, s_nan().total_cmp(&s_nan())); - assert_eq!(Ordering::Equal, q_nan().total_cmp(&q_nan())); - - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-s_nan())); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f32::INFINITY)); - assert_eq!(Ordering::Less, (-f32::INFINITY).total_cmp(&-f32::MAX)); - assert_eq!(Ordering::Less, (-f32::MAX).total_cmp(&-2.5)); - assert_eq!(Ordering::Less, (-2.5_f32).total_cmp(&-1.5)); - assert_eq!(Ordering::Less, (-1.5_f32).total_cmp(&-1.0)); - assert_eq!(Ordering::Less, (-1.0_f32).total_cmp(&-0.5)); - assert_eq!(Ordering::Less, (-0.5_f32).total_cmp(&-f32::MIN_POSITIVE)); - assert_eq!(Ordering::Less, (-f32::MIN_POSITIVE).total_cmp(&-max_subnorm())); - assert_eq!(Ordering::Less, (-max_subnorm()).total_cmp(&-min_subnorm())); - assert_eq!(Ordering::Less, (-min_subnorm()).total_cmp(&-0.0)); - assert_eq!(Ordering::Less, (-0.0_f32).total_cmp(&0.0)); - assert_eq!(Ordering::Less, 0.0_f32.total_cmp(&min_subnorm())); - assert_eq!(Ordering::Less, min_subnorm().total_cmp(&max_subnorm())); - assert_eq!(Ordering::Less, max_subnorm().total_cmp(&f32::MIN_POSITIVE)); - assert_eq!(Ordering::Less, f32::MIN_POSITIVE.total_cmp(&0.5)); - assert_eq!(Ordering::Less, 0.5_f32.total_cmp(&1.0)); - assert_eq!(Ordering::Less, 1.0_f32.total_cmp(&1.5)); - assert_eq!(Ordering::Less, 1.5_f32.total_cmp(&2.5)); - assert_eq!(Ordering::Less, 2.5_f32.total_cmp(&f32::MAX)); - assert_eq!(Ordering::Less, f32::MAX.total_cmp(&f32::INFINITY)); - assert_eq!(Ordering::Less, f32::INFINITY.total_cmp(&s_nan())); - assert_eq!(Ordering::Less, s_nan().total_cmp(&q_nan())); - - assert_eq!(Ordering::Greater, (-s_nan()).total_cmp(&-q_nan())); - assert_eq!(Ordering::Greater, (-f32::INFINITY).total_cmp(&-s_nan())); - assert_eq!(Ordering::Greater, (-f32::MAX).total_cmp(&-f32::INFINITY)); - assert_eq!(Ordering::Greater, (-2.5_f32).total_cmp(&-f32::MAX)); - assert_eq!(Ordering::Greater, (-1.5_f32).total_cmp(&-2.5)); - assert_eq!(Ordering::Greater, (-1.0_f32).total_cmp(&-1.5)); - assert_eq!(Ordering::Greater, (-0.5_f32).total_cmp(&-1.0)); - assert_eq!(Ordering::Greater, (-f32::MIN_POSITIVE).total_cmp(&-0.5)); - assert_eq!(Ordering::Greater, (-max_subnorm()).total_cmp(&-f32::MIN_POSITIVE)); - assert_eq!(Ordering::Greater, (-min_subnorm()).total_cmp(&-max_subnorm())); - assert_eq!(Ordering::Greater, (-0.0_f32).total_cmp(&-min_subnorm())); - assert_eq!(Ordering::Greater, 0.0_f32.total_cmp(&-0.0)); - assert_eq!(Ordering::Greater, min_subnorm().total_cmp(&0.0)); - assert_eq!(Ordering::Greater, max_subnorm().total_cmp(&min_subnorm())); - assert_eq!(Ordering::Greater, f32::MIN_POSITIVE.total_cmp(&max_subnorm())); - assert_eq!(Ordering::Greater, 0.5_f32.total_cmp(&f32::MIN_POSITIVE)); - assert_eq!(Ordering::Greater, 1.0_f32.total_cmp(&0.5)); - assert_eq!(Ordering::Greater, 1.5_f32.total_cmp(&1.0)); - assert_eq!(Ordering::Greater, 2.5_f32.total_cmp(&1.5)); - assert_eq!(Ordering::Greater, f32::MAX.total_cmp(&2.5)); - assert_eq!(Ordering::Greater, f32::INFINITY.total_cmp(&f32::MAX)); - assert_eq!(Ordering::Greater, s_nan().total_cmp(&f32::INFINITY)); - assert_eq!(Ordering::Greater, q_nan().total_cmp(&s_nan())); - - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-s_nan())); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-f32::INFINITY)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-f32::MAX)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-2.5)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-1.5)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-1.0)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-0.5)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-f32::MIN_POSITIVE)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-max_subnorm())); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-min_subnorm())); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-0.0)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&0.0)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&min_subnorm())); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&max_subnorm())); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&f32::MIN_POSITIVE)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&0.5)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&1.0)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&1.5)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&2.5)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&f32::MAX)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&f32::INFINITY)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&s_nan())); - - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f32::INFINITY)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f32::MAX)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-2.5)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-1.5)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-1.0)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-0.5)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f32::MIN_POSITIVE)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-max_subnorm())); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-min_subnorm())); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-0.0)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&0.0)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&min_subnorm())); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&max_subnorm())); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&f32::MIN_POSITIVE)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&0.5)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&1.0)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&1.5)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&2.5)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&f32::MAX)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&f32::INFINITY)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&s_nan())); -} - #[test] fn test_algebraic() { let a: f32 = 123.0; diff --git a/library/coretests/tests/floats/f64.rs b/library/coretests/tests/floats/f64.rs index 9d97a61e762c3..fea9cc19b39fa 100644 --- a/library/coretests/tests/floats/f64.rs +++ b/library/coretests/tests/floats/f64.rs @@ -105,149 +105,6 @@ fn test_float_bits_conv() { assert_eq!(f64::from_bits(masked_nan2).to_bits(), masked_nan2); } -#[test] -fn test_total_cmp() { - use core::cmp::Ordering; - - fn quiet_bit_mask() -> u64 { - 1 << (f64::MANTISSA_DIGITS - 2) - } - - fn min_subnorm() -> f64 { - f64::MIN_POSITIVE / f64::powf(2.0, f64::MANTISSA_DIGITS as f64 - 1.0) - } - - fn max_subnorm() -> f64 { - f64::MIN_POSITIVE - min_subnorm() - } - - fn q_nan() -> f64 { - f64::from_bits(f64::NAN.to_bits() | quiet_bit_mask()) - } - - fn s_nan() -> f64 { - f64::from_bits((f64::NAN.to_bits() & !quiet_bit_mask()) + 42) - } - - assert_eq!(Ordering::Equal, (-q_nan()).total_cmp(&-q_nan())); - assert_eq!(Ordering::Equal, (-s_nan()).total_cmp(&-s_nan())); - assert_eq!(Ordering::Equal, (-f64::INFINITY).total_cmp(&-f64::INFINITY)); - assert_eq!(Ordering::Equal, (-f64::MAX).total_cmp(&-f64::MAX)); - assert_eq!(Ordering::Equal, (-2.5_f64).total_cmp(&-2.5)); - assert_eq!(Ordering::Equal, (-1.0_f64).total_cmp(&-1.0)); - assert_eq!(Ordering::Equal, (-1.5_f64).total_cmp(&-1.5)); - assert_eq!(Ordering::Equal, (-0.5_f64).total_cmp(&-0.5)); - assert_eq!(Ordering::Equal, (-f64::MIN_POSITIVE).total_cmp(&-f64::MIN_POSITIVE)); - assert_eq!(Ordering::Equal, (-max_subnorm()).total_cmp(&-max_subnorm())); - assert_eq!(Ordering::Equal, (-min_subnorm()).total_cmp(&-min_subnorm())); - assert_eq!(Ordering::Equal, (-0.0_f64).total_cmp(&-0.0)); - assert_eq!(Ordering::Equal, 0.0_f64.total_cmp(&0.0)); - assert_eq!(Ordering::Equal, min_subnorm().total_cmp(&min_subnorm())); - assert_eq!(Ordering::Equal, max_subnorm().total_cmp(&max_subnorm())); - assert_eq!(Ordering::Equal, f64::MIN_POSITIVE.total_cmp(&f64::MIN_POSITIVE)); - assert_eq!(Ordering::Equal, 0.5_f64.total_cmp(&0.5)); - assert_eq!(Ordering::Equal, 1.0_f64.total_cmp(&1.0)); - assert_eq!(Ordering::Equal, 1.5_f64.total_cmp(&1.5)); - assert_eq!(Ordering::Equal, 2.5_f64.total_cmp(&2.5)); - assert_eq!(Ordering::Equal, f64::MAX.total_cmp(&f64::MAX)); - assert_eq!(Ordering::Equal, f64::INFINITY.total_cmp(&f64::INFINITY)); - assert_eq!(Ordering::Equal, s_nan().total_cmp(&s_nan())); - assert_eq!(Ordering::Equal, q_nan().total_cmp(&q_nan())); - - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-s_nan())); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f64::INFINITY)); - assert_eq!(Ordering::Less, (-f64::INFINITY).total_cmp(&-f64::MAX)); - assert_eq!(Ordering::Less, (-f64::MAX).total_cmp(&-2.5)); - assert_eq!(Ordering::Less, (-2.5_f64).total_cmp(&-1.5)); - assert_eq!(Ordering::Less, (-1.5_f64).total_cmp(&-1.0)); - assert_eq!(Ordering::Less, (-1.0_f64).total_cmp(&-0.5)); - assert_eq!(Ordering::Less, (-0.5_f64).total_cmp(&-f64::MIN_POSITIVE)); - assert_eq!(Ordering::Less, (-f64::MIN_POSITIVE).total_cmp(&-max_subnorm())); - assert_eq!(Ordering::Less, (-max_subnorm()).total_cmp(&-min_subnorm())); - assert_eq!(Ordering::Less, (-min_subnorm()).total_cmp(&-0.0)); - assert_eq!(Ordering::Less, (-0.0_f64).total_cmp(&0.0)); - assert_eq!(Ordering::Less, 0.0_f64.total_cmp(&min_subnorm())); - assert_eq!(Ordering::Less, min_subnorm().total_cmp(&max_subnorm())); - assert_eq!(Ordering::Less, max_subnorm().total_cmp(&f64::MIN_POSITIVE)); - assert_eq!(Ordering::Less, f64::MIN_POSITIVE.total_cmp(&0.5)); - assert_eq!(Ordering::Less, 0.5_f64.total_cmp(&1.0)); - assert_eq!(Ordering::Less, 1.0_f64.total_cmp(&1.5)); - assert_eq!(Ordering::Less, 1.5_f64.total_cmp(&2.5)); - assert_eq!(Ordering::Less, 2.5_f64.total_cmp(&f64::MAX)); - assert_eq!(Ordering::Less, f64::MAX.total_cmp(&f64::INFINITY)); - assert_eq!(Ordering::Less, f64::INFINITY.total_cmp(&s_nan())); - assert_eq!(Ordering::Less, s_nan().total_cmp(&q_nan())); - - assert_eq!(Ordering::Greater, (-s_nan()).total_cmp(&-q_nan())); - assert_eq!(Ordering::Greater, (-f64::INFINITY).total_cmp(&-s_nan())); - assert_eq!(Ordering::Greater, (-f64::MAX).total_cmp(&-f64::INFINITY)); - assert_eq!(Ordering::Greater, (-2.5_f64).total_cmp(&-f64::MAX)); - assert_eq!(Ordering::Greater, (-1.5_f64).total_cmp(&-2.5)); - assert_eq!(Ordering::Greater, (-1.0_f64).total_cmp(&-1.5)); - assert_eq!(Ordering::Greater, (-0.5_f64).total_cmp(&-1.0)); - assert_eq!(Ordering::Greater, (-f64::MIN_POSITIVE).total_cmp(&-0.5)); - assert_eq!(Ordering::Greater, (-max_subnorm()).total_cmp(&-f64::MIN_POSITIVE)); - assert_eq!(Ordering::Greater, (-min_subnorm()).total_cmp(&-max_subnorm())); - assert_eq!(Ordering::Greater, (-0.0_f64).total_cmp(&-min_subnorm())); - assert_eq!(Ordering::Greater, 0.0_f64.total_cmp(&-0.0)); - assert_eq!(Ordering::Greater, min_subnorm().total_cmp(&0.0)); - assert_eq!(Ordering::Greater, max_subnorm().total_cmp(&min_subnorm())); - assert_eq!(Ordering::Greater, f64::MIN_POSITIVE.total_cmp(&max_subnorm())); - assert_eq!(Ordering::Greater, 0.5_f64.total_cmp(&f64::MIN_POSITIVE)); - assert_eq!(Ordering::Greater, 1.0_f64.total_cmp(&0.5)); - assert_eq!(Ordering::Greater, 1.5_f64.total_cmp(&1.0)); - assert_eq!(Ordering::Greater, 2.5_f64.total_cmp(&1.5)); - assert_eq!(Ordering::Greater, f64::MAX.total_cmp(&2.5)); - assert_eq!(Ordering::Greater, f64::INFINITY.total_cmp(&f64::MAX)); - assert_eq!(Ordering::Greater, s_nan().total_cmp(&f64::INFINITY)); - assert_eq!(Ordering::Greater, q_nan().total_cmp(&s_nan())); - - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-s_nan())); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-f64::INFINITY)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-f64::MAX)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-2.5)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-1.5)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-1.0)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-0.5)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-f64::MIN_POSITIVE)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-max_subnorm())); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-min_subnorm())); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-0.0)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&0.0)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&min_subnorm())); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&max_subnorm())); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&f64::MIN_POSITIVE)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&0.5)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&1.0)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&1.5)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&2.5)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&f64::MAX)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&f64::INFINITY)); - assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&s_nan())); - - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f64::INFINITY)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f64::MAX)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-2.5)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-1.5)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-1.0)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-0.5)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f64::MIN_POSITIVE)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-max_subnorm())); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-min_subnorm())); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-0.0)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&0.0)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&min_subnorm())); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&max_subnorm())); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&f64::MIN_POSITIVE)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&0.5)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&1.0)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&1.5)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&2.5)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&f64::MAX)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&f64::INFINITY)); - assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&s_nan())); -} - #[test] fn test_algebraic() { let a: f64 = 123.0; diff --git a/library/coretests/tests/floats/mod.rs b/library/coretests/tests/floats/mod.rs index ad25b49d8a313..60c2914c4245b 100644 --- a/library/coretests/tests/floats/mod.rs +++ b/library/coretests/tests/floats/mod.rs @@ -1190,3 +1190,169 @@ float_test! { let _ = one.clamp(3.0, Float::NAN); } } + +float_test! { + name: total_cmp, + attrs: { + const: #[cfg(false)], + f16: #[cfg(all(not(miri), target_has_reliable_f16_math))], + f128: #[cfg(all(not(miri), target_has_reliable_f128_math))], + }, + test { + use core::cmp::Ordering; + + fn quiet_bit_mask() -> ::Int { + 1 << (Float::MANTISSA_DIGITS - 2) + } + + fn q_nan() -> Float { + Float::from_bits(Float::NAN.to_bits() | quiet_bit_mask()) + } + + assert_eq!(Ordering::Equal, Float::total_cmp(&-q_nan(), &-q_nan())); + assert_eq!(Ordering::Equal, Float::total_cmp(&-Float::INFINITY, &-Float::INFINITY)); + assert_eq!(Ordering::Equal, Float::total_cmp(&-Float::MAX, &-Float::MAX)); + assert_eq!(Ordering::Equal, Float::total_cmp(&-2.5, &-2.5)); + assert_eq!(Ordering::Equal, Float::total_cmp(&-1.0, &-1.0)); + assert_eq!(Ordering::Equal, Float::total_cmp(&-1.5, &-1.5)); + assert_eq!(Ordering::Equal, Float::total_cmp(&-0.5, &-0.5)); + assert_eq!(Ordering::Equal, Float::total_cmp(&-Float::MIN_POSITIVE, &-Float::MIN_POSITIVE)); + assert_eq!(Ordering::Equal, Float::total_cmp(&-Float::MAX_SUBNORMAL, &-Float::MAX_SUBNORMAL)); + assert_eq!(Ordering::Equal, Float::total_cmp(&-Float::TINY, &-Float::TINY)); + assert_eq!(Ordering::Equal, Float::total_cmp(&-0.0, &-0.0)); + assert_eq!(Ordering::Equal, Float::total_cmp(&0.0, &0.0)); + assert_eq!(Ordering::Equal, Float::total_cmp(&Float::TINY, &Float::TINY)); + assert_eq!(Ordering::Equal, Float::total_cmp(&Float::MAX_SUBNORMAL, &Float::MAX_SUBNORMAL)); + assert_eq!(Ordering::Equal, Float::total_cmp(&Float::MIN_POSITIVE, &Float::MIN_POSITIVE)); + assert_eq!(Ordering::Equal, Float::total_cmp(&0.5, &0.5)); + assert_eq!(Ordering::Equal, Float::total_cmp(&1.0, &1.0)); + assert_eq!(Ordering::Equal, Float::total_cmp(&1.5, &1.5)); + assert_eq!(Ordering::Equal, Float::total_cmp(&2.5, &2.5)); + assert_eq!(Ordering::Equal, Float::total_cmp(&Float::MAX, &Float::MAX)); + assert_eq!(Ordering::Equal, Float::total_cmp(&Float::INFINITY, &Float::INFINITY)); + assert_eq!(Ordering::Equal, Float::total_cmp(&q_nan(), &q_nan())); + + assert_eq!(Ordering::Less, Float::total_cmp(&-Float::INFINITY, &-Float::MAX)); + assert_eq!(Ordering::Less, Float::total_cmp(&-Float::MAX, &-2.5)); + assert_eq!(Ordering::Less, Float::total_cmp(&-2.5, &-1.5)); + assert_eq!(Ordering::Less, Float::total_cmp(&-1.5, &-1.0)); + assert_eq!(Ordering::Less, Float::total_cmp(&-1.0, &-0.5)); + assert_eq!(Ordering::Less, Float::total_cmp(&-0.5, &-Float::MIN_POSITIVE)); + assert_eq!(Ordering::Less, Float::total_cmp(&-Float::MIN_POSITIVE, &-Float::MAX_SUBNORMAL)); + assert_eq!(Ordering::Less, Float::total_cmp(&-Float::MAX_SUBNORMAL, &-Float::TINY)); + assert_eq!(Ordering::Less, Float::total_cmp(&-Float::TINY, &-0.0)); + assert_eq!(Ordering::Less, Float::total_cmp(&-0.0, &0.0)); + assert_eq!(Ordering::Less, Float::total_cmp(&0.0, &Float::TINY)); + assert_eq!(Ordering::Less, Float::total_cmp(&Float::TINY, &Float::MAX_SUBNORMAL)); + assert_eq!(Ordering::Less, Float::total_cmp(&Float::MAX_SUBNORMAL, &Float::MIN_POSITIVE)); + assert_eq!(Ordering::Less, Float::total_cmp(&Float::MIN_POSITIVE, &0.5)); + assert_eq!(Ordering::Less, Float::total_cmp(&0.5, &1.0)); + assert_eq!(Ordering::Less, Float::total_cmp(&1.0, &1.5)); + assert_eq!(Ordering::Less, Float::total_cmp(&1.5, &2.5)); + assert_eq!(Ordering::Less, Float::total_cmp(&2.5, &Float::MAX)); + assert_eq!(Ordering::Less, Float::total_cmp(&Float::MAX, &Float::INFINITY)); + + assert_eq!(Ordering::Greater, Float::total_cmp(&-Float::MAX, &-Float::INFINITY)); + assert_eq!(Ordering::Greater, Float::total_cmp(&-2.5, &-Float::MAX)); + assert_eq!(Ordering::Greater, Float::total_cmp(&-1.5, &-2.5)); + assert_eq!(Ordering::Greater, Float::total_cmp(&-1.0, &-1.5)); + assert_eq!(Ordering::Greater, Float::total_cmp(&-0.5, &-1.0)); + assert_eq!(Ordering::Greater, Float::total_cmp(&-Float::MIN_POSITIVE, &-0.5)); + assert_eq!(Ordering::Greater, Float::total_cmp(&-Float::MAX_SUBNORMAL, &-Float::MIN_POSITIVE)); + assert_eq!(Ordering::Greater, Float::total_cmp(&-Float::TINY, &-Float::MAX_SUBNORMAL)); + assert_eq!(Ordering::Greater, Float::total_cmp(&-0.0, &-Float::TINY)); + assert_eq!(Ordering::Greater, Float::total_cmp(&0.0, &-0.0)); + assert_eq!(Ordering::Greater, Float::total_cmp(&Float::TINY, &0.0)); + assert_eq!(Ordering::Greater, Float::total_cmp(&Float::MAX_SUBNORMAL, &Float::TINY)); + assert_eq!(Ordering::Greater, Float::total_cmp(&Float::MIN_POSITIVE, &Float::MAX_SUBNORMAL)); + assert_eq!(Ordering::Greater, Float::total_cmp(&0.5, &Float::MIN_POSITIVE)); + assert_eq!(Ordering::Greater, Float::total_cmp(&1.0, &0.5)); + assert_eq!(Ordering::Greater, Float::total_cmp(&1.5, &1.0)); + assert_eq!(Ordering::Greater, Float::total_cmp(&2.5, &1.5)); + assert_eq!(Ordering::Greater, Float::total_cmp(&Float::MAX, &2.5)); + assert_eq!(Ordering::Greater, Float::total_cmp(&Float::INFINITY, &Float::MAX)); + + assert_eq!(Ordering::Less, Float::total_cmp(&-q_nan(), &-Float::INFINITY)); + assert_eq!(Ordering::Less, Float::total_cmp(&-q_nan(), &-Float::MAX)); + assert_eq!(Ordering::Less, Float::total_cmp(&-q_nan(), &-2.5)); + assert_eq!(Ordering::Less, Float::total_cmp(&-q_nan(), &-1.5)); + assert_eq!(Ordering::Less, Float::total_cmp(&-q_nan(), &-1.0)); + assert_eq!(Ordering::Less, Float::total_cmp(&-q_nan(), &-0.5)); + assert_eq!(Ordering::Less, Float::total_cmp(&-q_nan(), &-Float::MIN_POSITIVE)); + assert_eq!(Ordering::Less, Float::total_cmp(&-q_nan(), &-Float::MAX_SUBNORMAL)); + assert_eq!(Ordering::Less, Float::total_cmp(&-q_nan(), &-Float::TINY)); + assert_eq!(Ordering::Less, Float::total_cmp(&-q_nan(), &-0.0)); + assert_eq!(Ordering::Less, Float::total_cmp(&-q_nan(), &0.0)); + assert_eq!(Ordering::Less, Float::total_cmp(&-q_nan(), &Float::TINY)); + assert_eq!(Ordering::Less, Float::total_cmp(&-q_nan(), &Float::MAX_SUBNORMAL)); + assert_eq!(Ordering::Less, Float::total_cmp(&-q_nan(), &Float::MIN_POSITIVE)); + assert_eq!(Ordering::Less, Float::total_cmp(&-q_nan(), &0.5)); + assert_eq!(Ordering::Less, Float::total_cmp(&-q_nan(), &1.0)); + assert_eq!(Ordering::Less, Float::total_cmp(&-q_nan(), &1.5)); + assert_eq!(Ordering::Less, Float::total_cmp(&-q_nan(), &2.5)); + assert_eq!(Ordering::Less, Float::total_cmp(&-q_nan(), &Float::MAX)); + assert_eq!(Ordering::Less, Float::total_cmp(&-q_nan(), &Float::INFINITY)); + + } +} + +// FIXME(f16): Tests involving sNaN are disabled because without optimizations, `total_cmp` is +// getting incorrectly lowered to code that includes a `extend`/`trunc` round trip, which quiets +// sNaNs. See: https://github.com/llvm/llvm-project/issues/104915 + +float_test! { + name: total_cmp_s_nan, + attrs: { + const: #[cfg(false)], + f16: #[cfg(false)], + f128: #[cfg(all(not(miri), target_has_reliable_f128_math))], + }, + test { + use core::cmp::Ordering; + + fn quiet_bit_mask() -> ::Int { + 1 << (Float::MANTISSA_DIGITS - 2) + } + + fn q_nan() -> Float { + Float::from_bits(Float::NAN.to_bits() | quiet_bit_mask()) + } + + fn s_nan() -> Float { + Float::from_bits((Float::NAN.to_bits() & !quiet_bit_mask()) + 42) + } + assert_eq!(Ordering::Equal, Float::total_cmp(&-s_nan(), &-s_nan())); + assert_eq!(Ordering::Equal, Float::total_cmp(&s_nan(), &s_nan())); + assert_eq!(Ordering::Less, Float::total_cmp(&-q_nan(), &-s_nan())); + assert_eq!(Ordering::Less, Float::total_cmp(&-s_nan(), &-Float::INFINITY)); + assert_eq!(Ordering::Less, Float::total_cmp(&Float::INFINITY, &s_nan())); + assert_eq!(Ordering::Less, Float::total_cmp(&s_nan(), &q_nan())); + assert_eq!(Ordering::Greater, Float::total_cmp(&-s_nan(), &-q_nan())); + assert_eq!(Ordering::Greater, Float::total_cmp(&-Float::INFINITY, &-s_nan())); + assert_eq!(Ordering::Greater, Float::total_cmp(&s_nan(), &Float::INFINITY)); + assert_eq!(Ordering::Greater, Float::total_cmp(&q_nan(), &s_nan())); + assert_eq!(Ordering::Less, Float::total_cmp(&-q_nan(), &-s_nan())); + assert_eq!(Ordering::Less, Float::total_cmp(&-q_nan(), &s_nan())); + assert_eq!(Ordering::Less, Float::total_cmp(&-s_nan(), &-Float::INFINITY)); + assert_eq!(Ordering::Less, Float::total_cmp(&-s_nan(), &-Float::MAX)); + assert_eq!(Ordering::Less, Float::total_cmp(&-s_nan(), &-2.5)); + assert_eq!(Ordering::Less, Float::total_cmp(&-s_nan(), &-1.5)); + assert_eq!(Ordering::Less, Float::total_cmp(&-s_nan(), &-1.0)); + assert_eq!(Ordering::Less, Float::total_cmp(&-s_nan(), &-0.5)); + assert_eq!(Ordering::Less, Float::total_cmp(&-s_nan(), &-Float::MIN_POSITIVE)); + assert_eq!(Ordering::Less, Float::total_cmp(&-s_nan(), &-Float::MAX_SUBNORMAL)); + assert_eq!(Ordering::Less, Float::total_cmp(&-s_nan(), &-Float::TINY)); + assert_eq!(Ordering::Less, Float::total_cmp(&-s_nan(), &-0.0)); + assert_eq!(Ordering::Less, Float::total_cmp(&-s_nan(), &0.0)); + assert_eq!(Ordering::Less, Float::total_cmp(&-s_nan(), &Float::TINY)); + assert_eq!(Ordering::Less, Float::total_cmp(&-s_nan(), &Float::MAX_SUBNORMAL)); + assert_eq!(Ordering::Less, Float::total_cmp(&-s_nan(), &Float::MIN_POSITIVE)); + assert_eq!(Ordering::Less, Float::total_cmp(&-s_nan(), &0.5)); + assert_eq!(Ordering::Less, Float::total_cmp(&-s_nan(), &1.0)); + assert_eq!(Ordering::Less, Float::total_cmp(&-s_nan(), &1.5)); + assert_eq!(Ordering::Less, Float::total_cmp(&-s_nan(), &2.5)); + assert_eq!(Ordering::Less, Float::total_cmp(&-s_nan(), &Float::MAX)); + assert_eq!(Ordering::Less, Float::total_cmp(&-s_nan(), &Float::INFINITY)); + assert_eq!(Ordering::Less, Float::total_cmp(&-s_nan(), &s_nan())); + } +} From 57cf40cd82592f004736d955e87253b4346ba2fc Mon Sep 17 00:00:00 2001 From: Roger Curley Date: Thu, 7 Aug 2025 22:51:46 -0400 Subject: [PATCH 26/47] Hoist zero and one out into TestableFloat --- library/coretests/tests/floats/mod.rs | 160 ++++++++++++-------------- 1 file changed, 72 insertions(+), 88 deletions(-) diff --git a/library/coretests/tests/floats/mod.rs b/library/coretests/tests/floats/mod.rs index 60c2914c4245b..2c2a07920d06c 100644 --- a/library/coretests/tests/floats/mod.rs +++ b/library/coretests/tests/floats/mod.rs @@ -6,6 +6,8 @@ trait TestableFloat { type Int; /// Set the default tolerance for float comparison based on the type. const APPROX: Self; + const ZERO: Self; + const ONE: Self; const MIN_POSITIVE_NORMAL: Self; const MAX_SUBNORMAL: Self; /// Smallest number @@ -23,6 +25,8 @@ trait TestableFloat { impl TestableFloat for f16 { type Int = u16; const APPROX: Self = 1e-3; + const ZERO: Self = 0.0; + const ONE: Self = 1.0; const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE; const MAX_SUBNORMAL: Self = Self::MIN_POSITIVE.next_down(); const TINY: Self = Self::from_bits(0x1); @@ -35,6 +39,8 @@ impl TestableFloat for f16 { impl TestableFloat for f32 { type Int = u32; const APPROX: Self = 1e-6; + const ZERO: Self = 0.0; + const ONE: Self = 1.0; const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE; const MAX_SUBNORMAL: Self = Self::MIN_POSITIVE.next_down(); const TINY: Self = Self::from_bits(0x1); @@ -47,6 +53,8 @@ impl TestableFloat for f32 { impl TestableFloat for f64 { type Int = u64; const APPROX: Self = 1e-6; + const ZERO: Self = 0.0; + const ONE: Self = 1.0; const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE; const MAX_SUBNORMAL: Self = Self::MIN_POSITIVE.next_down(); const TINY: Self = Self::from_bits(0x1); @@ -59,6 +67,8 @@ impl TestableFloat for f64 { impl TestableFloat for f128 { type Int = u128; const APPROX: Self = 1e-9; + const ZERO: Self = 0.0; + const ONE: Self = 1.0; const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE; const MAX_SUBNORMAL: Self = Self::MIN_POSITIVE.next_down(); const TINY: Self = Self::from_bits(0x1); @@ -378,15 +388,14 @@ float_test! { f128: #[cfg(any(miri, target_has_reliable_f128))], }, test { - let zero: Float = 0.0; - assert_biteq!(0.0, zero); - assert!(!zero.is_infinite()); - assert!(zero.is_finite()); - assert!(zero.is_sign_positive()); - assert!(!zero.is_sign_negative()); - assert!(!zero.is_nan()); - assert!(!zero.is_normal()); - assert!(matches!(zero.classify(), Fp::Zero)); + assert_biteq!(0.0, Float::ZERO); + assert!(!Float::ZERO.is_infinite()); + assert!(Float::ZERO.is_finite()); + assert!(Float::ZERO.is_sign_positive()); + assert!(!Float::ZERO.is_sign_negative()); + assert!(!Float::ZERO.is_nan()); + assert!(!Float::ZERO.is_normal()); + assert!(matches!(Float::ZERO.classify(), Fp::Zero)); } } @@ -417,15 +426,14 @@ float_test! { f128: #[cfg(any(miri, target_has_reliable_f128))], }, test { - let one: Float = 1.0; - assert_biteq!(1.0, one); - assert!(!one.is_infinite()); - assert!(one.is_finite()); - assert!(one.is_sign_positive()); - assert!(!one.is_sign_negative()); - assert!(!one.is_nan()); - assert!(one.is_normal()); - assert!(matches!(one.classify(), Fp::Normal)); + assert_biteq!(1.0, Float::ONE); + assert!(!Float::ONE.is_infinite()); + assert!(Float::ONE.is_finite()); + assert!(Float::ONE.is_sign_positive()); + assert!(!Float::ONE.is_sign_negative()); + assert!(!Float::ONE.is_nan()); + assert!(Float::ONE.is_normal()); + assert!(matches!(Float::ONE.classify(), Fp::Normal)); } } @@ -439,11 +447,10 @@ float_test! { let nan: Float = Float::NAN; let inf: Float = Float::INFINITY; let neg_inf: Float = Float::NEG_INFINITY; - let zero: Float = 0.0; let pos: Float = 5.3; let neg: Float = -10.732; assert!(nan.is_nan()); - assert!(!zero.is_nan()); + assert!(!Float::ZERO.is_nan()); assert!(!pos.is_nan()); assert!(!neg.is_nan()); assert!(!inf.is_nan()); @@ -461,13 +468,12 @@ float_test! { let nan: Float = Float::NAN; let inf: Float = Float::INFINITY; let neg_inf: Float = Float::NEG_INFINITY; - let zero: Float = 0.0; let pos: Float = 42.8; let neg: Float = -109.2; assert!(!nan.is_infinite()); assert!(inf.is_infinite()); assert!(neg_inf.is_infinite()); - assert!(!zero.is_infinite()); + assert!(!Float::ZERO.is_infinite()); assert!(!pos.is_infinite()); assert!(!neg.is_infinite()); } @@ -483,13 +489,12 @@ float_test! { let nan: Float = Float::NAN; let inf: Float = Float::INFINITY; let neg_inf: Float = Float::NEG_INFINITY; - let zero: Float = 0.0; let pos: Float = 42.8; let neg: Float = -109.2; assert!(!nan.is_finite()); assert!(!inf.is_finite()); assert!(!neg_inf.is_finite()); - assert!(zero.is_finite()); + assert!(Float::ZERO.is_finite()); assert!(pos.is_finite()); assert!(neg.is_finite()); } @@ -505,15 +510,13 @@ float_test! { let nan: Float = Float::NAN; let inf: Float = Float::INFINITY; let neg_inf: Float = Float::NEG_INFINITY; - let zero: Float = 0.0; let neg_zero: Float = -0.0; - let one : Float = 1.0; assert!(!nan.is_normal()); assert!(!inf.is_normal()); assert!(!neg_inf.is_normal()); - assert!(!zero.is_normal()); + assert!(!Float::ZERO.is_normal()); assert!(!neg_zero.is_normal()); - assert!(one.is_normal()); + assert!(Float::ONE.is_normal()); assert!(Float::MIN_POSITIVE_NORMAL.is_normal()); assert!(!Float::MAX_SUBNORMAL.is_normal()); } @@ -528,15 +531,13 @@ float_test! { let nan: Float = Float::NAN; let inf: Float = Float::INFINITY; let neg_inf: Float = Float::NEG_INFINITY; - let zero: Float = 0.0; let neg_zero: Float = -0.0; - let one: Float = 1.0; assert!(matches!(nan.classify(), Fp::Nan)); assert!(matches!(inf.classify(), Fp::Infinite)); assert!(matches!(neg_inf.classify(), Fp::Infinite)); - assert!(matches!(zero.classify(), Fp::Zero)); + assert!(matches!(Float::ZERO.classify(), Fp::Zero)); assert!(matches!(neg_zero.classify(), Fp::Zero)); - assert!(matches!(one.classify(), Fp::Normal)); + assert!(matches!(Float::ONE.classify(), Fp::Normal)); assert!(matches!(Float::MIN_POSITIVE_NORMAL.classify(), Fp::Normal)); assert!(matches!(Float::MAX_SUBNORMAL.classify(), Fp::Subnormal)); } @@ -756,15 +757,13 @@ float_test! { f128: #[cfg(any(miri, target_has_reliable_f128_math))], }, test { - let one: Float = 1.0; - let zero: Float = 0.0; assert_biteq!(Float::INFINITY.abs(), Float::INFINITY); - assert_biteq!(one.abs(), one); - assert_biteq!(zero.abs(), zero); - assert_biteq!((-zero).abs(), zero); - assert_biteq!((-one).abs(), one); + assert_biteq!(Float::ONE.abs(), Float::ONE); + assert_biteq!(Float::ZERO.abs(), Float::ZERO); + assert_biteq!((-Float::ZERO).abs(), Float::ZERO); + assert_biteq!((-Float::ONE).abs(), Float::ONE); assert_biteq!(Float::NEG_INFINITY.abs(), Float::INFINITY); - assert_biteq!((one / Float::NEG_INFINITY).abs(), zero); + assert_biteq!((Float::ONE / Float::NEG_INFINITY).abs(), Float::ZERO); assert!(Float::NAN.abs().is_nan()); } } @@ -1001,15 +1000,13 @@ float_test! { f128: #[cfg(any(miri, target_has_reliable_f128_math))], }, test { - let one: Float = 1.0; - let zero: Float = 0.0; - assert_biteq!(Float::INFINITY.signum(), one); - assert_biteq!(one.signum(), one); - assert_biteq!(zero.signum(), one); - assert_biteq!((-zero).signum(), -one); - assert_biteq!((-one).signum(), -one); - assert_biteq!(Float::NEG_INFINITY.signum(), -one); - assert_biteq!((one / Float::NEG_INFINITY).signum(), -one); + assert_biteq!(Float::INFINITY.signum(), Float::ONE); + assert_biteq!(Float::ONE.signum(), Float::ONE); + assert_biteq!(Float::ZERO.signum(), Float::ONE); + assert_biteq!((-Float::ZERO).signum(), -Float::ONE); + assert_biteq!((-Float::ONE).signum(), -Float::ONE); + assert_biteq!(Float::NEG_INFINITY.signum(), -Float::ONE); + assert_biteq!((Float::ONE / Float::NEG_INFINITY).signum(), -Float::ONE); assert!(Float::NAN.signum().is_nan()); } } @@ -1021,15 +1018,13 @@ float_test! { f128: #[cfg(any(miri, target_has_reliable_f128))], }, test { - let one: Float = 1.0; - let zero: Float = 0.0; assert!(Float::INFINITY.is_sign_positive()); - assert!(one.is_sign_positive()); - assert!(zero.is_sign_positive()); - assert!(!(-zero).is_sign_positive()); - assert!(!(-one).is_sign_positive()); + assert!(Float::ONE.is_sign_positive()); + assert!(Float::ZERO.is_sign_positive()); + assert!(!(-Float::ZERO).is_sign_positive()); + assert!(!(-Float::ONE).is_sign_positive()); assert!(!Float::NEG_INFINITY.is_sign_positive()); - assert!(!(one / Float::NEG_INFINITY).is_sign_positive()); + assert!(!(Float::ONE / Float::NEG_INFINITY).is_sign_positive()); assert!(Float::NAN.is_sign_positive()); assert!(!(-Float::NAN).is_sign_positive()); } @@ -1042,15 +1037,13 @@ float_test! { f128: #[cfg(any(miri, target_has_reliable_f128))], }, test { - let one: Float = 1.0; - let zero: Float = 0.0; assert!(!Float::INFINITY.is_sign_negative()); - assert!(!one.is_sign_negative()); - assert!(!zero.is_sign_negative()); - assert!((-zero).is_sign_negative()); - assert!((-one).is_sign_negative()); + assert!(!Float::ONE.is_sign_negative()); + assert!(!Float::ZERO.is_sign_negative()); + assert!((-Float::ZERO).is_sign_negative()); + assert!((-Float::ONE).is_sign_negative()); assert!(Float::NEG_INFINITY.is_sign_negative()); - assert!((one / Float::NEG_INFINITY).is_sign_negative()); + assert!((Float::ONE / Float::NEG_INFINITY).is_sign_negative()); assert!(!Float::NAN.is_sign_negative()); assert!((-Float::NAN).is_sign_negative()); } @@ -1063,19 +1056,17 @@ float_test! { f128: #[cfg(any(miri, target_has_reliable_f128))], }, test { - let one: Float = 1.0; - let zero: Float = 0.0; assert_biteq!(Float::NEG_INFINITY.next_up(), Float::MIN); assert_biteq!(Float::MIN.next_up(), -Float::MAX_DOWN); - assert_biteq!((-one - Float::EPSILON).next_up(), -one); + assert_biteq!((-Float::ONE - Float::EPSILON).next_up(), -Float::ONE); assert_biteq!((-Float::MIN_POSITIVE_NORMAL).next_up(), -Float::MAX_SUBNORMAL); assert_biteq!((-Float::TINY_UP).next_up(), -Float::TINY); - assert_biteq!((-Float::TINY).next_up(), -zero); - assert_biteq!((-zero).next_up(), Float::TINY); - assert_biteq!(zero.next_up(), Float::TINY); + assert_biteq!((-Float::TINY).next_up(), -Float::ZERO); + assert_biteq!((-Float::ZERO).next_up(), Float::TINY); + assert_biteq!(Float::ZERO.next_up(), Float::TINY); assert_biteq!(Float::TINY.next_up(), Float::TINY_UP); assert_biteq!(Float::MAX_SUBNORMAL.next_up(), Float::MIN_POSITIVE_NORMAL); - assert_biteq!(one.next_up(), 1.0 + Float::EPSILON); + assert_biteq!(Float::ONE.next_up(), 1.0 + Float::EPSILON); assert_biteq!(Float::MAX.next_up(), Float::INFINITY); assert_biteq!(Float::INFINITY.next_up(), Float::INFINITY); @@ -1096,20 +1087,18 @@ float_test! { f128: #[cfg(any(miri, target_has_reliable_f128))], }, test { - let one: Float = 1.0; - let zero: Float = 0.0; assert_biteq!(Float::NEG_INFINITY.next_down(), Float::NEG_INFINITY); assert_biteq!(Float::MIN.next_down(), Float::NEG_INFINITY); assert_biteq!((-Float::MAX_DOWN).next_down(), Float::MIN); - assert_biteq!((-one).next_down(), -1.0 - Float::EPSILON); + assert_biteq!((-Float::ONE).next_down(), -1.0 - Float::EPSILON); assert_biteq!((-Float::MAX_SUBNORMAL).next_down(), -Float::MIN_POSITIVE_NORMAL); assert_biteq!((-Float::TINY).next_down(), -Float::TINY_UP); - assert_biteq!((-zero).next_down(), -Float::TINY); - assert_biteq!((zero).next_down(), -Float::TINY); - assert_biteq!(Float::TINY.next_down(), zero); + assert_biteq!((-Float::ZERO).next_down(), -Float::TINY); + assert_biteq!((Float::ZERO).next_down(), -Float::TINY); + assert_biteq!(Float::TINY.next_down(), Float::ZERO); assert_biteq!(Float::TINY_UP.next_down(), Float::TINY); assert_biteq!(Float::MIN_POSITIVE_NORMAL.next_down(), Float::MAX_SUBNORMAL); - assert_biteq!((1.0 + Float::EPSILON).next_down(), one); + assert_biteq!((1.0 + Float::EPSILON).next_down(), Float::ONE); assert_biteq!(Float::MAX.next_down(), Float::MAX_DOWN); assert_biteq!(Float::INFINITY.next_down(), Float::MAX); @@ -1134,14 +1123,12 @@ float_test! { f128: #[cfg(all(not(miri), target_has_reliable_f128_math))], }, test { - let one: Float = 1.0; - let zero: Float = 0.0; assert!(Float::NAN.sqrt().is_nan()); assert!(Float::NEG_INFINITY.sqrt().is_nan()); - assert!((-one).sqrt().is_nan()); - assert_biteq!((-zero).sqrt(), -zero); - assert_biteq!(zero.sqrt(), zero); - assert_biteq!(one.sqrt(), one); + assert!((-Float::ONE).sqrt().is_nan()); + assert_biteq!((-Float::ZERO).sqrt(), -Float::ZERO); + assert_biteq!(Float::ZERO.sqrt(), Float::ZERO); + assert_biteq!(Float::ONE.sqrt(), Float::ONE); assert_biteq!(Float::INFINITY.sqrt(), Float::INFINITY); } } @@ -1156,8 +1143,7 @@ float_test! { f128: #[should_panic, cfg(any(miri, target_has_reliable_f128))], }, test { - let one : Float = 1.0; - let _ = one.clamp(3.0, 1.0); + let _ = Float::ONE.clamp(3.0, 1.0); } } @@ -1171,8 +1157,7 @@ float_test! { f128: #[should_panic, cfg(any(miri, target_has_reliable_f128))], }, test { - let one : Float = 1.0; - let _ = one.clamp(Float::NAN, 1.0); + let _ = Float::ONE.clamp(Float::NAN, 1.0); } } @@ -1186,8 +1171,7 @@ float_test! { f128: #[should_panic, cfg(any(miri, target_has_reliable_f128))], }, test { - let one : Float = 1.0; - let _ = one.clamp(3.0, Float::NAN); + let _ = Float::ONE.clamp(3.0, Float::NAN); } } From 347cd4e6cb8a189c12a89e89a3951a5b1043308c Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 8 Aug 2025 14:04:17 +1000 Subject: [PATCH 27/47] Augment the test. Some cases that are currently handled incorrectly. --- tests/ui/stats/macro-stats.rs | 30 ++++++++++++++++++++++++++---- tests/ui/stats/macro-stats.stderr | 7 ++++++- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/tests/ui/stats/macro-stats.rs b/tests/ui/stats/macro-stats.rs index d986904ddd679..1b7d58bf0585c 100644 --- a/tests/ui/stats/macro-stats.rs +++ b/tests/ui/stats/macro-stats.rs @@ -49,10 +49,32 @@ fn opt(x: Option) { } } -macro_rules! long_name_that_fits_on_a_single_line { - () => {} -} -long_name_that_fits_on_a_single_line!(); +macro_rules! long_name_that_fits_on_one_line { () => {} } +long_name_that_fits_on_one_line!(); +long_name_that_fits_on_one_line!(); +long_name_that_fits_on_one_line!(); +long_name_that_fits_on_one_line!(); +long_name_that_fits_on_one_line!(); +long_name_that_fits_on_one_line!(); +long_name_that_fits_on_one_line!(); +long_name_that_fits_on_one_line!(); +long_name_that_fits_on_one_line!(); +long_name_that_fits_on_one_line!(); + +macro_rules! long_name_that_fits_on_one_line_ { () => {} } +long_name_that_fits_on_one_line_!(); + +macro_rules! long_name_that_fits_on_one_line__ { () => {} } +long_name_that_fits_on_one_line__!(); + +macro_rules! long_name_that_fits_on_one_line___ { () => {} } +long_name_that_fits_on_one_line___!(); + +macro_rules! long_name_that_fits_on_one_line____ { () => {} } +long_name_that_fits_on_one_line____!(); + +macro_rules! long_name_that_fits_on_one_line_____ { () => {} } +long_name_that_fits_on_one_line_____!(); macro_rules! long_name_that_doesnt_fit_on_one_line { ($t:ty) => { diff --git a/tests/ui/stats/macro-stats.stderr b/tests/ui/stats/macro-stats.stderr index 8d0fdb8958a8d..0ef9adc6dd405 100644 --- a/tests/ui/stats/macro-stats.stderr +++ b/tests/ui/stats/macro-stats.stderr @@ -22,6 +22,11 @@ macro-stats trait_tys! 1 2 2.0 macro-stats n99! 2 2 1.0 4 2.0 macro-stats none! 1 1 1.0 4 4.0 macro-stats u32! 1 1 1.0 3 3.0 -macro-stats long_name_that_fits_on_a_single_line! 1 1 1.0 0 0.0 +macro-stats long_name_that_fits_on_one_line! 10 10 1.0 0 0.0 +macro-stats long_name_that_fits_on_one_line_____! 1 1 1.0 0 0.0 +macro-stats long_name_that_fits_on_one_line____! 1 1 1.0 0 0.0 +macro-stats long_name_that_fits_on_one_line___! 1 1 1.0 0 0.0 +macro-stats long_name_that_fits_on_one_line__! 1 1 1.0 0 0.0 +macro-stats long_name_that_fits_on_one_line_! 1 1 1.0 0 0.0 macro-stats #[test] 1 1 1.0 0 0.0 macro-stats =================================================================================== From 0239e464876c8785e8601712220f17d9c58bcba6 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 8 Aug 2025 14:10:54 +1000 Subject: [PATCH 28/47] Fix some bad formatting in `-Zmacro-stats` output. I also double-checked that everything looks good on some real-world crates. --- compiler/rustc_interface/src/passes.rs | 2 +- tests/ui/stats/macro-stats.stderr | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 8dec8069bc7bd..8bbd0e614921e 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -371,7 +371,7 @@ fn print_macro_stats(ecx: &ExtCtxt<'_>) { // The name won't abut or overlap with the uses value, but it does // overlap with the empty part of the uses column. Shrink the width // of the uses column to account for the excess name length. - uses_w = uses_with_underscores.len() + 1 + uses_w -= name.len() - name_w; }; _ = writeln!( diff --git a/tests/ui/stats/macro-stats.stderr b/tests/ui/stats/macro-stats.stderr index 0ef9adc6dd405..75b90df6466ff 100644 --- a/tests/ui/stats/macro-stats.stderr +++ b/tests/ui/stats/macro-stats.stderr @@ -22,11 +22,11 @@ macro-stats trait_tys! 1 2 2.0 macro-stats n99! 2 2 1.0 4 2.0 macro-stats none! 1 1 1.0 4 4.0 macro-stats u32! 1 1 1.0 3 3.0 -macro-stats long_name_that_fits_on_one_line! 10 10 1.0 0 0.0 +macro-stats long_name_that_fits_on_one_line! 10 10 1.0 0 0.0 macro-stats long_name_that_fits_on_one_line_____! 1 1 1.0 0 0.0 -macro-stats long_name_that_fits_on_one_line____! 1 1 1.0 0 0.0 -macro-stats long_name_that_fits_on_one_line___! 1 1 1.0 0 0.0 -macro-stats long_name_that_fits_on_one_line__! 1 1 1.0 0 0.0 -macro-stats long_name_that_fits_on_one_line_! 1 1 1.0 0 0.0 +macro-stats long_name_that_fits_on_one_line____! 1 1 1.0 0 0.0 +macro-stats long_name_that_fits_on_one_line___! 1 1 1.0 0 0.0 +macro-stats long_name_that_fits_on_one_line__! 1 1 1.0 0 0.0 +macro-stats long_name_that_fits_on_one_line_! 1 1 1.0 0 0.0 macro-stats #[test] 1 1 1.0 0 0.0 macro-stats =================================================================================== From 997c6a88216ac6a11bb864ab92fa4cf3da72f019 Mon Sep 17 00:00:00 2001 From: Amogh Shivaram Date: Fri, 8 Aug 2025 00:20:55 -0500 Subject: [PATCH 29/47] Escape diff strings in graphviz --- compiler/rustc_mir_dataflow/src/framework/graphviz.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/compiler/rustc_mir_dataflow/src/framework/graphviz.rs b/compiler/rustc_mir_dataflow/src/framework/graphviz.rs index a7d5422a3d721..f86a15a8f9215 100644 --- a/compiler/rustc_mir_dataflow/src/framework/graphviz.rs +++ b/compiler/rustc_mir_dataflow/src/framework/graphviz.rs @@ -800,6 +800,7 @@ where let re = regex!("\t?\u{001f}([+-])"); let raw_diff = format!("{:#?}", DebugDiffWithAdapter { new, old, ctxt }); + let raw_diff = dot::escape_html(&raw_diff); // Replace newlines in the `Debug` output with `
` let raw_diff = raw_diff.replace('\n', r#"
"#); From 3abe5f9dbef7046d8c63b7c2f782ebe6142881ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Fri, 8 Aug 2025 08:56:51 +0200 Subject: [PATCH 30/47] Add snapshot test for cross-compilation cargo build --- src/bootstrap/src/core/builder/tests.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/bootstrap/src/core/builder/tests.rs b/src/bootstrap/src/core/builder/tests.rs index 5361347da9047..41fdb905e6a97 100644 --- a/src/bootstrap/src/core/builder/tests.rs +++ b/src/bootstrap/src/core/builder/tests.rs @@ -975,6 +975,21 @@ mod snapshot { .render_steps(), @"[build] rustc 0 -> cargo 1 "); } + #[test] + fn build_cargo_cross() { + let ctx = TestCtx::new(); + insta::assert_snapshot!( + ctx.config("build") + .paths(&["cargo"]) + .hosts(&[TEST_TRIPLE_1]) + .render_steps(), @r" + [build] llvm + [build] rustc 0 -> rustc 1 + [build] rustc 1 -> std 1 + [build] rustc 1 -> cargo 2 + "); + } + #[test] fn dist_default_stage() { let ctx = TestCtx::new(); From 9eb9b5a191807e823ade467902f5e663f6ba9341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Fri, 8 Aug 2025 08:57:44 +0200 Subject: [PATCH 31/47] Build host library when cross-compiling `ToolTarget` tools --- src/bootstrap/src/core/build_steps/tool.rs | 8 ++++++-- src/bootstrap/src/core/builder/tests.rs | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/bootstrap/src/core/build_steps/tool.rs b/src/bootstrap/src/core/build_steps/tool.rs index 7b0ef942abecf..aa00cd03c5bb7 100644 --- a/src/bootstrap/src/core/build_steps/tool.rs +++ b/src/bootstrap/src/core/build_steps/tool.rs @@ -334,7 +334,8 @@ pub fn prepare_tool_cargo( /// Determines how to build a `ToolTarget`, i.e. which compiler should be used to compile it. /// The compiler stage is automatically bumped if we need to cross-compile a stage 1 tool. pub enum ToolTargetBuildMode { - /// Build the tool using rustc that corresponds to the selected CLI stage. + /// Build the tool for the given `target` using rustc that corresponds to the top CLI + /// stage. Build(TargetSelection), /// Build the tool so that it can be attached to the sysroot of the passed compiler. /// Since we always dist stage 2+, the compiler that builds the tool in this case has to be @@ -366,7 +367,10 @@ pub(crate) fn get_tool_target_compiler( } else { // If we are cross-compiling a stage 1 tool, we cannot do that with a stage 0 compiler, // so we auto-bump the tool's stage to 2, which means we need a stage 1 compiler. - builder.compiler(build_compiler_stage.max(1), builder.host_target) + let build_compiler = builder.compiler(build_compiler_stage.max(1), builder.host_target); + // We also need the host stdlib to compile host code (proc macros/build scripts) + builder.std(build_compiler, builder.host_target); + build_compiler }; builder.std(compiler, target); compiler diff --git a/src/bootstrap/src/core/builder/tests.rs b/src/bootstrap/src/core/builder/tests.rs index 41fdb905e6a97..1914e24fa35a1 100644 --- a/src/bootstrap/src/core/builder/tests.rs +++ b/src/bootstrap/src/core/builder/tests.rs @@ -985,6 +985,7 @@ mod snapshot { .render_steps(), @r" [build] llvm [build] rustc 0 -> rustc 1 + [build] rustc 1 -> std 1 [build] rustc 1 -> std 1 [build] rustc 1 -> cargo 2 "); From 7671b5af71deb6c43cb466b333c821b03eb98b65 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sat, 2 Aug 2025 22:33:04 +1000 Subject: [PATCH 32/47] coverage: Extract HIR-related helper code out of the main module --- .../src/coverage/hir_info.rs | 125 +++++++++++++++ .../src/coverage/mappings.rs | 2 +- .../rustc_mir_transform/src/coverage/mod.rs | 145 ++---------------- .../rustc_mir_transform/src/coverage/spans.rs | 3 +- 4 files changed, 139 insertions(+), 136 deletions(-) create mode 100644 compiler/rustc_mir_transform/src/coverage/hir_info.rs diff --git a/compiler/rustc_mir_transform/src/coverage/hir_info.rs b/compiler/rustc_mir_transform/src/coverage/hir_info.rs new file mode 100644 index 0000000000000..7d720159295bc --- /dev/null +++ b/compiler/rustc_mir_transform/src/coverage/hir_info.rs @@ -0,0 +1,125 @@ +use rustc_hir as hir; +use rustc_hir::intravisit::{Visitor, walk_expr}; +use rustc_middle::hir::nested_filter; +use rustc_middle::ty::TyCtxt; +use rustc_span::Span; +use rustc_span::def_id::LocalDefId; + +/// Function information extracted from HIR by the coverage instrumentor. +#[derive(Debug)] +pub(crate) struct ExtractedHirInfo { + pub(crate) function_source_hash: u64, + pub(crate) is_async_fn: bool, + /// The span of the function's signature, if available. + /// Must have the same context and filename as the body span. + pub(crate) fn_sig_span: Option, + pub(crate) body_span: Span, + /// "Holes" are regions within the function body (or its expansions) that + /// should not be included in coverage spans for this function + /// (e.g. closures and nested items). + pub(crate) hole_spans: Vec, +} + +pub(crate) fn extract_hir_info<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> ExtractedHirInfo { + // FIXME(#79625): Consider improving MIR to provide the information needed, to avoid going back + // to HIR for it. + + // HACK: For synthetic MIR bodies (async closures), use the def id of the HIR body. + if tcx.is_synthetic_mir(def_id) { + return extract_hir_info(tcx, tcx.local_parent(def_id)); + } + + let hir_node = tcx.hir_node_by_def_id(def_id); + let fn_body_id = hir_node.body_id().expect("HIR node is a function with body"); + let hir_body = tcx.hir_body(fn_body_id); + + let maybe_fn_sig = hir_node.fn_sig(); + let is_async_fn = maybe_fn_sig.is_some_and(|fn_sig| fn_sig.header.is_async()); + + let mut body_span = hir_body.value.span; + + use hir::{Closure, Expr, ExprKind, Node}; + // Unexpand a closure's body span back to the context of its declaration. + // This helps with closure bodies that consist of just a single bang-macro, + // and also with closure bodies produced by async desugaring. + if let Node::Expr(&Expr { kind: ExprKind::Closure(&Closure { fn_decl_span, .. }), .. }) = + hir_node + { + body_span = body_span.find_ancestor_in_same_ctxt(fn_decl_span).unwrap_or(body_span); + } + + // The actual signature span is only used if it has the same context and + // filename as the body, and precedes the body. + let fn_sig_span = maybe_fn_sig.map(|fn_sig| fn_sig.span).filter(|&fn_sig_span| { + let source_map = tcx.sess.source_map(); + let file_idx = |span: Span| source_map.lookup_source_file_idx(span.lo()); + + fn_sig_span.eq_ctxt(body_span) + && fn_sig_span.hi() <= body_span.lo() + && file_idx(fn_sig_span) == file_idx(body_span) + }); + + let function_source_hash = hash_mir_source(tcx, hir_body); + + let hole_spans = extract_hole_spans_from_hir(tcx, hir_body); + + ExtractedHirInfo { function_source_hash, is_async_fn, fn_sig_span, body_span, hole_spans } +} + +fn hash_mir_source<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &'tcx hir::Body<'tcx>) -> u64 { + // FIXME(cjgillot) Stop hashing HIR manually here. + let owner = hir_body.id().hir_id.owner; + tcx.hir_owner_nodes(owner).opt_hash_including_bodies.unwrap().to_smaller_hash().as_u64() +} + +fn extract_hole_spans_from_hir<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &hir::Body<'tcx>) -> Vec { + struct HolesVisitor<'tcx> { + tcx: TyCtxt<'tcx>, + hole_spans: Vec, + } + + impl<'tcx> Visitor<'tcx> for HolesVisitor<'tcx> { + /// We have special handling for nested items, but we still want to + /// traverse into nested bodies of things that are not considered items, + /// such as "anon consts" (e.g. array lengths). + type NestedFilter = nested_filter::OnlyBodies; + + fn maybe_tcx(&mut self) -> TyCtxt<'tcx> { + self.tcx + } + + /// We override `visit_nested_item` instead of `visit_item` because we + /// only need the item's span, not the item itself. + fn visit_nested_item(&mut self, id: hir::ItemId) -> Self::Result { + let span = self.tcx.def_span(id.owner_id.def_id); + self.visit_hole_span(span); + // Having visited this item, we don't care about its children, + // so don't call `walk_item`. + } + + // We override `visit_expr` instead of the more specific expression + // visitors, so that we have direct access to the expression span. + fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) { + match expr.kind { + hir::ExprKind::Closure(_) | hir::ExprKind::ConstBlock(_) => { + self.visit_hole_span(expr.span); + // Having visited this expression, we don't care about its + // children, so don't call `walk_expr`. + } + + // For other expressions, recursively visit as normal. + _ => walk_expr(self, expr), + } + } + } + impl HolesVisitor<'_> { + fn visit_hole_span(&mut self, hole_span: Span) { + self.hole_spans.push(hole_span); + } + } + + let mut visitor = HolesVisitor { tcx, hole_spans: vec![] }; + + visitor.visit_body(hir_body); + visitor.hole_spans +} diff --git a/compiler/rustc_mir_transform/src/coverage/mappings.rs b/compiler/rustc_mir_transform/src/coverage/mappings.rs index 399978b5915a0..46fe7c40826a8 100644 --- a/compiler/rustc_mir_transform/src/coverage/mappings.rs +++ b/compiler/rustc_mir_transform/src/coverage/mappings.rs @@ -4,8 +4,8 @@ use rustc_middle::mir::{self, BasicBlock, StatementKind}; use rustc_middle::ty::TyCtxt; use rustc_span::Span; -use crate::coverage::ExtractedHirInfo; use crate::coverage::graph::{BasicCoverageBlock, CoverageGraph}; +use crate::coverage::hir_info::ExtractedHirInfo; use crate::coverage::spans::extract_refined_covspans; use crate::coverage::unexpand::unexpand_into_body_span; diff --git a/compiler/rustc_mir_transform/src/coverage/mod.rs b/compiler/rustc_mir_transform/src/coverage/mod.rs index f6945a95a7c38..47a87a2e94d8b 100644 --- a/compiler/rustc_mir_transform/src/coverage/mod.rs +++ b/compiler/rustc_mir_transform/src/coverage/mod.rs @@ -1,26 +1,22 @@ -mod counters; -mod graph; -mod mappings; -pub(super) mod query; -mod spans; -#[cfg(test)] -mod tests; -mod unexpand; - -use rustc_hir as hir; -use rustc_hir::intravisit::{Visitor, walk_expr}; -use rustc_middle::hir::nested_filter; use rustc_middle::mir::coverage::{CoverageKind, FunctionCoverageInfo, Mapping, MappingKind}; use rustc_middle::mir::{self, BasicBlock, Statement, StatementKind, TerminatorKind}; use rustc_middle::ty::TyCtxt; -use rustc_span::Span; -use rustc_span::def_id::LocalDefId; use tracing::{debug, debug_span, trace}; use crate::coverage::counters::BcbCountersData; use crate::coverage::graph::CoverageGraph; use crate::coverage::mappings::ExtractedMappings; +mod counters; +mod graph; +mod hir_info; +mod mappings; +pub(super) mod query; +mod spans; +#[cfg(test)] +mod tests; +mod unexpand; + /// Inserts `StatementKind::Coverage` statements that either instrument the binary with injected /// counters, via intrinsic `llvm.instrprof.increment`, and/or inject metadata used during codegen /// to construct the coverage map. @@ -67,7 +63,7 @@ fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir: let def_id = mir_body.source.def_id(); let _span = debug_span!("instrument_function_for_coverage", ?def_id).entered(); - let hir_info = extract_hir_info(tcx, def_id.expect_local()); + let hir_info = hir_info::extract_hir_info(tcx, def_id.expect_local()); // Build the coverage graph, which is a simplified view of the MIR control-flow // graph that ignores some details not relevant to coverage instrumentation. @@ -147,122 +143,3 @@ fn inject_statement(mir_body: &mut mir::Body<'_>, counter_kind: CoverageKind, bb let statement = Statement::new(source_info, StatementKind::Coverage(counter_kind)); data.statements.insert(0, statement); } - -/// Function information extracted from HIR by the coverage instrumentor. -#[derive(Debug)] -struct ExtractedHirInfo { - function_source_hash: u64, - is_async_fn: bool, - /// The span of the function's signature, if available. - /// Must have the same context and filename as the body span. - fn_sig_span: Option, - body_span: Span, - /// "Holes" are regions within the function body (or its expansions) that - /// should not be included in coverage spans for this function - /// (e.g. closures and nested items). - hole_spans: Vec, -} - -fn extract_hir_info<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> ExtractedHirInfo { - // FIXME(#79625): Consider improving MIR to provide the information needed, to avoid going back - // to HIR for it. - - // HACK: For synthetic MIR bodies (async closures), use the def id of the HIR body. - if tcx.is_synthetic_mir(def_id) { - return extract_hir_info(tcx, tcx.local_parent(def_id)); - } - - let hir_node = tcx.hir_node_by_def_id(def_id); - let fn_body_id = hir_node.body_id().expect("HIR node is a function with body"); - let hir_body = tcx.hir_body(fn_body_id); - - let maybe_fn_sig = hir_node.fn_sig(); - let is_async_fn = maybe_fn_sig.is_some_and(|fn_sig| fn_sig.header.is_async()); - - let mut body_span = hir_body.value.span; - - use hir::{Closure, Expr, ExprKind, Node}; - // Unexpand a closure's body span back to the context of its declaration. - // This helps with closure bodies that consist of just a single bang-macro, - // and also with closure bodies produced by async desugaring. - if let Node::Expr(&Expr { kind: ExprKind::Closure(&Closure { fn_decl_span, .. }), .. }) = - hir_node - { - body_span = body_span.find_ancestor_in_same_ctxt(fn_decl_span).unwrap_or(body_span); - } - - // The actual signature span is only used if it has the same context and - // filename as the body, and precedes the body. - let fn_sig_span = maybe_fn_sig.map(|fn_sig| fn_sig.span).filter(|&fn_sig_span| { - let source_map = tcx.sess.source_map(); - let file_idx = |span: Span| source_map.lookup_source_file_idx(span.lo()); - - fn_sig_span.eq_ctxt(body_span) - && fn_sig_span.hi() <= body_span.lo() - && file_idx(fn_sig_span) == file_idx(body_span) - }); - - let function_source_hash = hash_mir_source(tcx, hir_body); - - let hole_spans = extract_hole_spans_from_hir(tcx, hir_body); - - ExtractedHirInfo { function_source_hash, is_async_fn, fn_sig_span, body_span, hole_spans } -} - -fn hash_mir_source<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &'tcx hir::Body<'tcx>) -> u64 { - // FIXME(cjgillot) Stop hashing HIR manually here. - let owner = hir_body.id().hir_id.owner; - tcx.hir_owner_nodes(owner).opt_hash_including_bodies.unwrap().to_smaller_hash().as_u64() -} - -fn extract_hole_spans_from_hir<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &hir::Body<'tcx>) -> Vec { - struct HolesVisitor<'tcx> { - tcx: TyCtxt<'tcx>, - hole_spans: Vec, - } - - impl<'tcx> Visitor<'tcx> for HolesVisitor<'tcx> { - /// We have special handling for nested items, but we still want to - /// traverse into nested bodies of things that are not considered items, - /// such as "anon consts" (e.g. array lengths). - type NestedFilter = nested_filter::OnlyBodies; - - fn maybe_tcx(&mut self) -> TyCtxt<'tcx> { - self.tcx - } - - /// We override `visit_nested_item` instead of `visit_item` because we - /// only need the item's span, not the item itself. - fn visit_nested_item(&mut self, id: hir::ItemId) -> Self::Result { - let span = self.tcx.def_span(id.owner_id.def_id); - self.visit_hole_span(span); - // Having visited this item, we don't care about its children, - // so don't call `walk_item`. - } - - // We override `visit_expr` instead of the more specific expression - // visitors, so that we have direct access to the expression span. - fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) { - match expr.kind { - hir::ExprKind::Closure(_) | hir::ExprKind::ConstBlock(_) => { - self.visit_hole_span(expr.span); - // Having visited this expression, we don't care about its - // children, so don't call `walk_expr`. - } - - // For other expressions, recursively visit as normal. - _ => walk_expr(self, expr), - } - } - } - impl HolesVisitor<'_> { - fn visit_hole_span(&mut self, hole_span: Span) { - self.hole_spans.push(hole_span); - } - } - - let mut visitor = HolesVisitor { tcx, hole_spans: vec![] }; - - visitor.visit_body(hir_body); - visitor.hole_spans -} diff --git a/compiler/rustc_mir_transform/src/coverage/spans.rs b/compiler/rustc_mir_transform/src/coverage/spans.rs index 0ee42abb19564..ae9459dee842d 100644 --- a/compiler/rustc_mir_transform/src/coverage/spans.rs +++ b/compiler/rustc_mir_transform/src/coverage/spans.rs @@ -7,8 +7,9 @@ use rustc_span::{BytePos, DesugaringKind, ExpnKind, MacroKind, Span}; use tracing::instrument; use crate::coverage::graph::{BasicCoverageBlock, CoverageGraph}; +use crate::coverage::hir_info::ExtractedHirInfo; use crate::coverage::spans::from_mir::{Hole, RawSpanFromMir, SpanFromMir}; -use crate::coverage::{ExtractedHirInfo, mappings, unexpand}; +use crate::coverage::{mappings, unexpand}; mod from_mir; From db9f0bb5326b8726c2760ffb2f17977118534c2c Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sat, 2 Aug 2025 22:39:54 +1000 Subject: [PATCH 33/47] coverage: Remove obsolete comment about hashing HIR This code does not hash HIR manually (and has not done so for some time); it merely obtains a hash returned as part of `hir_owner_nodes`. --- compiler/rustc_mir_transform/src/coverage/hir_info.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/hir_info.rs b/compiler/rustc_mir_transform/src/coverage/hir_info.rs index 7d720159295bc..28fdc52b06cb9 100644 --- a/compiler/rustc_mir_transform/src/coverage/hir_info.rs +++ b/compiler/rustc_mir_transform/src/coverage/hir_info.rs @@ -67,9 +67,12 @@ pub(crate) fn extract_hir_info<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> E } fn hash_mir_source<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &'tcx hir::Body<'tcx>) -> u64 { - // FIXME(cjgillot) Stop hashing HIR manually here. let owner = hir_body.id().hir_id.owner; - tcx.hir_owner_nodes(owner).opt_hash_including_bodies.unwrap().to_smaller_hash().as_u64() + tcx.hir_owner_nodes(owner) + .opt_hash_including_bodies + .expect("hash should be present when coverage instrumentation is enabled") + .to_smaller_hash() + .as_u64() } fn extract_hole_spans_from_hir<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &hir::Body<'tcx>) -> Vec { From ac862c0ffb7b9df22b24603925ce299f8638b712 Mon Sep 17 00:00:00 2001 From: lcnr Date: Fri, 8 Aug 2025 13:49:27 +0200 Subject: [PATCH 34/47] fix typos MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Rémy Rakic --- .../ui/impl-trait/member-constraints/incomplete-constraint.rs | 4 ++-- .../reject-choice-due-to-prev-constraint.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/ui/impl-trait/member-constraints/incomplete-constraint.rs b/tests/ui/impl-trait/member-constraints/incomplete-constraint.rs index 92fd31516d72d..4c085cc1eedf8 100644 --- a/tests/ui/impl-trait/member-constraints/incomplete-constraint.rs +++ b/tests/ui/impl-trait/member-constraints/incomplete-constraint.rs @@ -1,8 +1,8 @@ //@ check-pass // FIXME(-Znext-solver): enable this test -// These functions currently does not normalize the opaque type but will do -// so in the future. At this point we've got a new use of the opaque will fully +// These functions currently do not normalize the opaque type but will do +// so in the future. At this point we've got a new use of the opaque with fully // universal arguments but for which lifetimes in the hidden type are unconstrained. // // Applying the member constraints would then incompletely infer `'unconstrained` to `'static`. diff --git a/tests/ui/impl-trait/member-constraints/reject-choice-due-to-prev-constraint.rs b/tests/ui/impl-trait/member-constraints/reject-choice-due-to-prev-constraint.rs index 4018256b2c84e..33f2d277fe58c 100644 --- a/tests/ui/impl-trait/member-constraints/reject-choice-due-to-prev-constraint.rs +++ b/tests/ui/impl-trait/member-constraints/reject-choice-due-to-prev-constraint.rs @@ -8,7 +8,7 @@ // As '0 gets outlived by 'a - its "upper bound" - the only applicable choice // region is 'a. // -// '1 has to outlive 'b so the only applicabel choice regions are 'b and 'static. +// '1 has to outlive 'b so the only applicable choice regions are 'b and 'static. // Considering this member constraint by itself would choose 'b as it is the // smaller of the two regions. // From 988569f3371f7cba75d91c46bece11813b4ace4f Mon Sep 17 00:00:00 2001 From: lcnr Date: Fri, 8 Aug 2025 15:29:37 +0200 Subject: [PATCH 35/47] remove unnecessary `TypeFoldable` impls --- compiler/rustc_middle/src/ty/mod.rs | 2 ++ compiler/rustc_type_ir/src/canonical.rs | 1 - compiler/rustc_type_ir/src/const_kind.rs | 7 +------ compiler/rustc_type_ir/src/infer_ctxt.rs | 2 -- 4 files changed, 3 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 8c0277055cd2c..73e1661106eaf 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -1031,6 +1031,8 @@ pub struct ParamEnvAnd<'tcx, T> { #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable)] #[derive(TypeVisitable, TypeFoldable)] pub struct TypingEnv<'tcx> { + #[type_foldable(identity)] + #[type_visitable(ignore)] pub typing_mode: TypingMode<'tcx>, pub param_env: ParamEnv<'tcx>, } diff --git a/compiler/rustc_type_ir/src/canonical.rs b/compiler/rustc_type_ir/src/canonical.rs index c66a83662d769..0cb8d8902c6ff 100644 --- a/compiler/rustc_type_ir/src/canonical.rs +++ b/compiler/rustc_type_ir/src/canonical.rs @@ -199,7 +199,6 @@ impl CanonicalVarKind { /// usize or f32). In order to faithfully reproduce a type, we need to /// know what set of types a given type variable can be unified with. #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] -#[derive(TypeVisitable_Generic, TypeFoldable_Generic)] #[cfg_attr( feature = "nightly", derive(Decodable_NoContext, Encodable_NoContext, HashStable_NoContext) diff --git a/compiler/rustc_type_ir/src/const_kind.rs b/compiler/rustc_type_ir/src/const_kind.rs index 4be38d4e702d7..70a8509b51339 100644 --- a/compiler/rustc_type_ir/src/const_kind.rs +++ b/compiler/rustc_type_ir/src/const_kind.rs @@ -92,15 +92,10 @@ rustc_index::newtype_index! { /// An inference variable for a const, for use in const generics. #[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)] -#[derive(TypeVisitable_Generic, TypeFoldable_Generic)] #[cfg_attr(feature = "nightly", derive(Encodable_NoContext, Decodable_NoContext))] pub enum InferConst { /// Infer the value of the const. - Var( - #[type_foldable(identity)] - #[type_visitable(ignore)] - ConstVid, - ), + Var(ConstVid), /// A fresh const variable. See `infer::freshen` for more details. Fresh(u32), } diff --git a/compiler/rustc_type_ir/src/infer_ctxt.rs b/compiler/rustc_type_ir/src/infer_ctxt.rs index b4873c8c71cbb..e6462a97f8ab6 100644 --- a/compiler/rustc_type_ir/src/infer_ctxt.rs +++ b/compiler/rustc_type_ir/src/infer_ctxt.rs @@ -1,7 +1,6 @@ use derive_where::derive_where; #[cfg(feature = "nightly")] use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable_NoContext}; -use rustc_type_ir_macros::{TypeFoldable_Generic, TypeVisitable_Generic}; use crate::fold::TypeFoldable; use crate::inherent::*; @@ -20,7 +19,6 @@ use crate::{self as ty, Interner}; /// If neither of these functions are available, feel free to reach out to /// t-types for help. #[derive_where(Clone, Copy, Hash, PartialEq, Eq, Debug; I: Interner)] -#[derive(TypeVisitable_Generic, TypeFoldable_Generic)] #[cfg_attr( feature = "nightly", derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext) From c3e504d399a51781f328d4195c79ce9b7d44388d Mon Sep 17 00:00:00 2001 From: Spxg Date: Fri, 8 Aug 2025 21:27:46 +0800 Subject: [PATCH 36/47] Fix wasm target build with atomics feature --- library/std/src/sys/pal/wasm/atomics/thread.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/library/std/src/sys/pal/wasm/atomics/thread.rs b/library/std/src/sys/pal/wasm/atomics/thread.rs index ebfabaafc794d..42a7dbdf8b8b0 100644 --- a/library/std/src/sys/pal/wasm/atomics/thread.rs +++ b/library/std/src/sys/pal/wasm/atomics/thread.rs @@ -56,6 +56,10 @@ impl Thread { pub fn join(self) {} } +pub(crate) fn current_os_id() -> Option { + None +} + pub fn available_parallelism() -> io::Result> { unsupported() } From dbc6f5836c798ceea7518696a1cb72b8f602df76 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Fri, 8 Aug 2025 23:11:08 +0800 Subject: [PATCH 37/47] rustc_metadata: remove unused private trait impls --- .../src/rmeta/decoder/cstore_impl.rs | 18 ------ .../rustc_metadata/src/rmeta/parameterized.rs | 7 +-- compiler/rustc_metadata/src/rmeta/table.rs | 63 ------------------- 3 files changed, 1 insertion(+), 87 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 0f3896fd9be8a..df95ed602cd36 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -74,24 +74,6 @@ impl<'a, 'tcx, T: Copy + Decodable>> ProcessQueryValue<' } } -impl<'a, 'tcx, T: Copy + Decodable>> - ProcessQueryValue<'tcx, ty::EarlyBinder<'tcx, &'tcx [T]>> - for Option> -{ - #[inline(always)] - fn process_decoded( - self, - tcx: TyCtxt<'tcx>, - err: impl Fn() -> !, - ) -> ty::EarlyBinder<'tcx, &'tcx [T]> { - ty::EarlyBinder::bind(if let Some(iter) = self { - tcx.arena.alloc_from_iter(iter) - } else { - err() - }) - } -} - impl<'a, 'tcx, T: Copy + Decodable>> ProcessQueryValue<'tcx, Option<&'tcx [T]>> for Option> { diff --git a/compiler/rustc_metadata/src/rmeta/parameterized.rs b/compiler/rustc_metadata/src/rmeta/parameterized.rs index d632e65104a7a..34180001f8044 100644 --- a/compiler/rustc_metadata/src/rmeta/parameterized.rs +++ b/compiler/rustc_metadata/src/rmeta/parameterized.rs @@ -6,7 +6,7 @@ use rustc_index::{Idx, IndexVec}; use rustc_middle::ty::{Binder, EarlyBinder}; use rustc_span::Symbol; -use crate::rmeta::{LazyArray, LazyTable, LazyValue}; +use crate::rmeta::{LazyArray, LazyValue}; pub(crate) trait ParameterizedOverTcx: 'static { type Value<'tcx>; @@ -48,10 +48,6 @@ impl ParameterizedOverTcx for LazyArray { type Value<'tcx> = LazyArray>; } -impl ParameterizedOverTcx for LazyTable { - type Value<'tcx> = LazyTable>; -} - macro_rules! trivially_parameterized_over_tcx { ($($ty:ty),+ $(,)?) => { $( @@ -154,7 +150,6 @@ parameterized_over_tcx! { rustc_middle::mir::CoroutineLayout, rustc_middle::mir::interpret::ConstAllocation, rustc_middle::ty::Clause, - rustc_middle::ty::ClauseKind, rustc_middle::ty::Const, rustc_middle::ty::ConstConditions, rustc_middle::ty::FnSig, diff --git a/compiler/rustc_metadata/src/rmeta/table.rs b/compiler/rustc_metadata/src/rmeta/table.rs index 28f406fbc9629..0671aa203993a 100644 --- a/compiler/rustc_metadata/src/rmeta/table.rs +++ b/compiler/rustc_metadata/src/rmeta/table.rs @@ -66,22 +66,6 @@ pub(super) trait FixedSizeEncoding: IsDefault { fn write_to_bytes(self, b: &mut Self::ByteArray); } -/// This implementation is not used generically, but for reading/writing -/// concrete `u32` fields in `Lazy*` structures, which may be zero. -impl FixedSizeEncoding for u32 { - type ByteArray = [u8; 4]; - - #[inline] - fn from_bytes(b: &[u8; 4]) -> Self { - Self::from_le_bytes(*b) - } - - #[inline] - fn write_to_bytes(self, b: &mut [u8; 4]) { - *b = self.to_le_bytes(); - } -} - impl FixedSizeEncoding for u64 { type ByteArray = [u8; 8]; @@ -174,14 +158,6 @@ fixed_size_enum! { } } -fixed_size_enum! { - ty::ImplPolarity { - ( Positive ) - ( Negative ) - ( Reservation ) - } -} - fixed_size_enum! { hir::Constness { ( NotConst ) @@ -306,45 +282,6 @@ impl FixedSizeEncoding for bool { } } -impl FixedSizeEncoding for Option { - type ByteArray = [u8; 1]; - - #[inline] - fn from_bytes(b: &[u8; 1]) -> Self { - match b[0] { - 0 => Some(false), - 1 => Some(true), - 2 => None, - _ => unreachable!(), - } - } - - #[inline] - fn write_to_bytes(self, b: &mut [u8; 1]) { - debug_assert!(!self.is_default()); - b[0] = match self { - Some(false) => 0, - Some(true) => 1, - None => 2, - }; - } -} - -impl FixedSizeEncoding for UnusedGenericParams { - type ByteArray = [u8; 4]; - - #[inline] - fn from_bytes(b: &[u8; 4]) -> Self { - let x: u32 = u32::from_bytes(b); - UnusedGenericParams::from_bits(x) - } - - #[inline] - fn write_to_bytes(self, b: &mut [u8; 4]) { - self.bits().write_to_bytes(b); - } -} - // NOTE(eddyb) there could be an impl for `usize`, which would enable a more // generic `LazyValue` impl, but in the general case we might not need / want // to fit every `usize` in `u32`. From 5e8ebd5ecd8546591a6707ac9e1a3b8a64c72f76 Mon Sep 17 00:00:00 2001 From: binarycat Date: Tue, 10 Jun 2025 13:35:46 -0500 Subject: [PATCH 38/47] rustdoc: IndexItem::{stability -> is_unstable} --- src/librustdoc/formats/cache.rs | 2 +- src/librustdoc/html/render/mod.rs | 10 ++-------- src/librustdoc/html/render/search_index.rs | 4 ++-- 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index c3251ca780627..d0dbc5a74da88 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -586,7 +586,7 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It search_type, aliases, deprecation, - stability: item.stability(tcx), + is_unstable: item.stability(tcx).map(|x| x.is_unstable()).unwrap_or(false), }; cache.search_index.push(index_item); } diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 29917bb0fca2a..81c05d93f8232 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -50,8 +50,7 @@ use std::{fs, str}; use askama::Template; use itertools::Either; use rustc_attr_data_structures::{ - ConstStability, DeprecatedSince, Deprecation, RustcVersion, Stability, StabilityLevel, - StableSince, + ConstStability, DeprecatedSince, Deprecation, RustcVersion, StabilityLevel, StableSince, }; use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet}; use rustc_hir::Mutability; @@ -141,12 +140,7 @@ pub(crate) struct IndexItem { pub(crate) search_type: Option, pub(crate) aliases: Box<[Symbol]>, pub(crate) deprecation: Option, - pub(crate) stability: Option, -} -impl IndexItem { - fn is_unstable(&self) -> bool { - matches!(&self.stability, Some(Stability { level: StabilityLevel::Unstable { .. }, .. })) - } + pub(crate) is_unstable: bool, } /// A type used for the search index. diff --git a/src/librustdoc/html/render/search_index.rs b/src/librustdoc/html/render/search_index.rs index 6635aa02e97d1..a0911bf58736e 100644 --- a/src/librustdoc/html/render/search_index.rs +++ b/src/librustdoc/html/render/search_index.rs @@ -93,7 +93,7 @@ pub(crate) fn build_index( ), aliases: item.attrs.get_doc_aliases(), deprecation: item.deprecation(tcx), - stability: item.stability(tcx), + is_unstable: item.stability(tcx).is_some_and(|x| x.is_unstable()), }); } } @@ -700,7 +700,7 @@ pub(crate) fn build_index( // bitmasks always use 1-indexing for items, with 0 as the crate itself deprecated.push(u32::try_from(index + 1).unwrap()); } - if item.is_unstable() { + if item.is_unstable { unstable.push(u32::try_from(index + 1).unwrap()); } } From fdbc8d08a63a3d34b7aebabb2f18a768462a98c4 Mon Sep 17 00:00:00 2001 From: binarycat Date: Fri, 8 Aug 2025 11:55:00 -0500 Subject: [PATCH 39/47] rustdoc search: add performance note about searchIndexUnstable check --- src/librustdoc/html/static/js/search.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index da15433622afe..1ed789f63d5d3 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -3291,6 +3291,12 @@ class DocSearch { } // sort unstable items later + // FIXME: there is some doubt if this is the most effecient way to implement this. + // alternative options include: + // * put is_unstable on each item when the index is built. + // increases memory usage but avoids a hashmap lookup. + // * put is_unstable on each item before sorting. + // better worst case performance but worse average case performance. a = Number( // @ts-expect-error this.searchIndexUnstable.get(aaa.item.crate).contains(aaa.item.bitIndex), From 8b95291cd4f0a6e838fec89cb2687d81aff237be Mon Sep 17 00:00:00 2001 From: lcnr Date: Fri, 8 Aug 2025 19:16:03 +0200 Subject: [PATCH 40/47] borrowck: move error tainting earlier --- compiler/rustc_borrowck/src/handle_placeholders.rs | 13 +------------ compiler/rustc_borrowck/src/type_check/mod.rs | 11 +++++++++++ compiler/rustc_borrowck/src/universal_regions.rs | 10 +++++----- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_borrowck/src/handle_placeholders.rs b/compiler/rustc_borrowck/src/handle_placeholders.rs index aaaf2f45c869d..a0962e9b0151a 100644 --- a/compiler/rustc_borrowck/src/handle_placeholders.rs +++ b/compiler/rustc_borrowck/src/handle_placeholders.rs @@ -216,22 +216,11 @@ pub(crate) fn compute_sccs_applying_placeholder_outlives_constraints<'tcx>( placeholder_index_to_region: _, liveness_constraints, mut outlives_constraints, - mut member_constraints, + member_constraints, universe_causes, type_tests, } = constraints; - if let Some(guar) = universal_regions.tainted_by_errors() { - debug!("Universal regions tainted by errors; removing constraints!"); - // Suppress unhelpful extra errors in `infer_opaque_types` by clearing out all - // outlives bounds that we may end up checking. - outlives_constraints = Default::default(); - member_constraints = Default::default(); - - // Also taint the entire scope. - infcx.set_tainted_by_errors(guar); - } - let fr_static = universal_regions.fr_static; let compute_sccs = |constraints: &OutlivesConstraintSet<'tcx>, diff --git a/compiler/rustc_borrowck/src/type_check/mod.rs b/compiler/rustc_borrowck/src/type_check/mod.rs index 148d0de3bab28..c3aa205d5aab3 100644 --- a/compiler/rustc_borrowck/src/type_check/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/mod.rs @@ -182,6 +182,17 @@ pub(crate) fn type_check<'tcx>( ) }); + // In case type check encountered an error region, we suppress unhelpful extra + // errors in by clearing out all outlives bounds that we may end up checking. + if let Some(guar) = universal_region_relations.universal_regions.encountered_re_error() { + debug!("encountered an error region; removing constraints!"); + constraints.outlives_constraints = Default::default(); + constraints.member_constraints = Default::default(); + constraints.type_tests = Default::default(); + root_cx.set_tainted_by_errors(guar); + infcx.set_tainted_by_errors(guar); + } + MirTypeckResults { constraints, universal_region_relations, diff --git a/compiler/rustc_borrowck/src/universal_regions.rs b/compiler/rustc_borrowck/src/universal_regions.rs index 240c9a5223be0..296a273553336 100644 --- a/compiler/rustc_borrowck/src/universal_regions.rs +++ b/compiler/rustc_borrowck/src/universal_regions.rs @@ -217,7 +217,7 @@ struct UniversalRegionIndices<'tcx> { /// Whether we've encountered an error region. If we have, cancel all /// outlives errors, as they are likely bogus. - pub tainted_by_errors: Cell>, + pub encountered_re_error: Cell>, } #[derive(Debug, PartialEq)] @@ -442,8 +442,8 @@ impl<'tcx> UniversalRegions<'tcx> { self.fr_fn_body } - pub(crate) fn tainted_by_errors(&self) -> Option { - self.indices.tainted_by_errors.get() + pub(crate) fn encountered_re_error(&self) -> Option { + self.indices.encountered_re_error.get() } } @@ -706,7 +706,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { UniversalRegionIndices { indices: global_mapping.chain(arg_mapping).collect(), fr_static, - tainted_by_errors: Cell::new(None), + encountered_re_error: Cell::new(None), } } @@ -916,7 +916,7 @@ impl<'tcx> UniversalRegionIndices<'tcx> { match r.kind() { ty::ReVar(..) => r.as_var(), ty::ReError(guar) => { - self.tainted_by_errors.set(Some(guar)); + self.encountered_re_error.set(Some(guar)); // We use the `'static` `RegionVid` because `ReError` doesn't actually exist in the // `UniversalRegionIndices`. This is fine because 1) it is a fallback only used if // errors are being emitted and 2) it leaves the happy path unaffected. From 4eee55691aeb345cbac0ad558003d5f2ed72b478 Mon Sep 17 00:00:00 2001 From: lcnr Date: Fri, 8 Aug 2025 19:18:17 +0200 Subject: [PATCH 41/47] borrowck: defer opaque type errors --- .../rustc_borrowck/src/diagnostics/mod.rs | 2 +- ...{opaque_suggestions.rs => opaque_types.rs} | 19 +++++ .../src/diagnostics/region_errors.rs | 3 - compiler/rustc_borrowck/src/lib.rs | 8 +- compiler/rustc_borrowck/src/nll.rs | 5 -- .../rustc_borrowck/src/region_infer/mod.rs | 2 +- .../src/region_infer/opaque_types.rs | 78 ++++++++++--------- tests/crashes/135528.rs | 20 ----- .../type-error-post-normalization-test.rs | 24 ++++++ .../type-error-post-normalization-test.stderr | 14 ++++ .../higher-ranked-regions-basic.rs | 2 + .../higher-ranked-regions-basic.stderr | 28 +++++-- .../ui/type-alias-impl-trait/hkl_forbidden.rs | 8 +- .../hkl_forbidden.stderr | 30 +++++-- .../type-alias-impl-trait/param_mismatch2.rs | 4 +- .../param_mismatch2.stderr | 13 +++- .../type-alias-impl-trait/param_mismatch3.rs | 4 +- .../param_mismatch3.stderr | 13 +++- 18 files changed, 193 insertions(+), 84 deletions(-) rename compiler/rustc_borrowck/src/diagnostics/{opaque_suggestions.rs => opaque_types.rs} (93%) delete mode 100644 tests/crashes/135528.rs create mode 100644 tests/ui/impl-trait/issues/type-error-post-normalization-test.rs create mode 100644 tests/ui/impl-trait/issues/type-error-post-normalization-test.stderr diff --git a/compiler/rustc_borrowck/src/diagnostics/mod.rs b/compiler/rustc_borrowck/src/diagnostics/mod.rs index 34bed375cb965..b67dba3af96d2 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mod.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mod.rs @@ -51,7 +51,7 @@ mod conflict_errors; mod explain_borrow; mod move_errors; mod mutability_errors; -mod opaque_suggestions; +mod opaque_types; mod region_errors; pub(crate) use bound_region_errors::{ToUniverseInfo, UniverseInfo}; diff --git a/compiler/rustc_borrowck/src/diagnostics/opaque_suggestions.rs b/compiler/rustc_borrowck/src/diagnostics/opaque_types.rs similarity index 93% rename from compiler/rustc_borrowck/src/diagnostics/opaque_suggestions.rs rename to compiler/rustc_borrowck/src/diagnostics/opaque_types.rs index 7192a889adcbc..83fe4a9f98f49 100644 --- a/compiler/rustc_borrowck/src/diagnostics/opaque_suggestions.rs +++ b/compiler/rustc_borrowck/src/diagnostics/opaque_types.rs @@ -18,9 +18,28 @@ use rustc_trait_selection::errors::impl_trait_overcapture_suggestion; use crate::MirBorrowckCtxt; use crate::borrow_set::BorrowData; use crate::consumers::RegionInferenceContext; +use crate::region_infer::opaque_types::DeferredOpaqueTypeError; use crate::type_check::Locations; impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { + pub(crate) fn report_opaque_type_errors(&mut self, errors: Vec>) { + if errors.is_empty() { + return; + } + let mut guar = None; + for error in errors { + guar = Some(match error { + DeferredOpaqueTypeError::InvalidOpaqueTypeArgs(err) => err.report(self.infcx), + DeferredOpaqueTypeError::LifetimeMismatchOpaqueParam(err) => { + self.infcx.dcx().emit_err(err) + } + }); + } + let guar = guar.unwrap(); + self.root_cx.set_tainted_by_errors(guar); + self.infcx.set_tainted_by_errors(guar); + } + /// Try to note when an opaque is involved in a borrowck error and that /// opaque captures lifetimes due to edition 2024. // FIXME: This code is otherwise somewhat general, and could easily be adapted diff --git a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs index b130cf8ed2764..2b74f1a48f731 100644 --- a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs @@ -92,9 +92,6 @@ impl<'tcx> RegionErrors<'tcx> { ) -> impl Iterator, ErrorGuaranteed)> { self.0.into_iter() } - pub(crate) fn has_errors(&self) -> Option { - self.0.get(0).map(|x| x.1) - } } impl std::fmt::Debug for RegionErrors<'_> { diff --git a/compiler/rustc_borrowck/src/lib.rs b/compiler/rustc_borrowck/src/lib.rs index 752ff8e6f586b..d76d6a04e6e03 100644 --- a/compiler/rustc_borrowck/src/lib.rs +++ b/compiler/rustc_borrowck/src/lib.rs @@ -375,7 +375,7 @@ fn do_mir_borrowck<'tcx>( polonius_context, ); - regioncx.infer_opaque_types(root_cx, &infcx, opaque_type_values); + let opaque_type_errors = regioncx.infer_opaque_types(root_cx, &infcx, opaque_type_values); // Dump MIR results into a file, if that is enabled. This lets us // write unit-tests, as well as helping with debugging. @@ -471,7 +471,11 @@ fn do_mir_borrowck<'tcx>( }; // Compute and report region errors, if any. - mbcx.report_region_errors(nll_errors); + if nll_errors.is_empty() { + mbcx.report_opaque_type_errors(opaque_type_errors); + } else { + mbcx.report_region_errors(nll_errors); + } let (mut flow_analysis, flow_entry_states) = get_flow_results(tcx, body, &move_data, &borrow_set, ®ioncx); diff --git a/compiler/rustc_borrowck/src/nll.rs b/compiler/rustc_borrowck/src/nll.rs index ca6092e70d253..8608a8a3a66f3 100644 --- a/compiler/rustc_borrowck/src/nll.rs +++ b/compiler/rustc_borrowck/src/nll.rs @@ -148,11 +148,6 @@ pub(crate) fn compute_regions<'tcx>( let (closure_region_requirements, nll_errors) = regioncx.solve(infcx, body, polonius_output.clone()); - if let Some(guar) = nll_errors.has_errors() { - // Suppress unhelpful extra errors in `infer_opaque_types`. - infcx.set_tainted_by_errors(guar); - } - NllOutput { regioncx, polonius_input: polonius_facts.map(Box::new), diff --git a/compiler/rustc_borrowck/src/region_infer/mod.rs b/compiler/rustc_borrowck/src/region_infer/mod.rs index f13200485332f..f4dc9627b3596 100644 --- a/compiler/rustc_borrowck/src/region_infer/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/mod.rs @@ -44,7 +44,7 @@ use crate::{ mod dump_mir; mod graphviz; -mod opaque_types; +pub(crate) mod opaque_types; mod reverse_sccs; pub(crate) mod values; diff --git a/compiler/rustc_borrowck/src/region_infer/opaque_types.rs b/compiler/rustc_borrowck/src/region_infer/opaque_types.rs index 6270e6d9a60ee..23c4554aa155f 100644 --- a/compiler/rustc_borrowck/src/region_infer/opaque_types.rs +++ b/compiler/rustc_borrowck/src/region_infer/opaque_types.rs @@ -6,7 +6,9 @@ use rustc_middle::ty::{ TypeVisitableExt, fold_regions, }; use rustc_span::Span; -use rustc_trait_selection::opaque_types::check_opaque_type_parameter_valid; +use rustc_trait_selection::opaque_types::{ + InvalidOpaqueTypeArgs, check_opaque_type_parameter_valid, +}; use tracing::{debug, instrument}; use super::RegionInferenceContext; @@ -14,6 +16,11 @@ use crate::BorrowCheckRootCtxt; use crate::session_diagnostics::LifetimeMismatchOpaqueParam; use crate::universal_regions::RegionClassification; +pub(crate) enum DeferredOpaqueTypeError<'tcx> { + InvalidOpaqueTypeArgs(InvalidOpaqueTypeArgs<'tcx>), + LifetimeMismatchOpaqueParam(LifetimeMismatchOpaqueParam<'tcx>), +} + impl<'tcx> RegionInferenceContext<'tcx> { /// Resolve any opaque types that were encountered while borrow checking /// this item. This is then used to get the type in the `type_of` query. @@ -58,13 +65,14 @@ impl<'tcx> RegionInferenceContext<'tcx> { /// /// [rustc-dev-guide chapter]: /// https://rustc-dev-guide.rust-lang.org/opaque-types-region-infer-restrictions.html - #[instrument(level = "debug", skip(self, root_cx, infcx), ret)] + #[instrument(level = "debug", skip(self, root_cx, infcx))] pub(crate) fn infer_opaque_types( &self, root_cx: &mut BorrowCheckRootCtxt<'tcx>, infcx: &InferCtxt<'tcx>, opaque_ty_decls: FxIndexMap, OpaqueHiddenType<'tcx>>, - ) { + ) -> Vec> { + let mut errors = Vec::new(); let mut decls_modulo_regions: FxIndexMap, (OpaqueTypeKey<'tcx>, Span)> = FxIndexMap::default(); @@ -124,8 +132,15 @@ impl<'tcx> RegionInferenceContext<'tcx> { }); debug!(?concrete_type); - let ty = - infcx.infer_opaque_definition_from_instantiation(opaque_type_key, concrete_type); + let ty = match infcx + .infer_opaque_definition_from_instantiation(opaque_type_key, concrete_type) + { + Ok(ty) => ty, + Err(err) => { + errors.push(DeferredOpaqueTypeError::InvalidOpaqueTypeArgs(err)); + continue; + } + }; // Sometimes, when the hidden type is an inference variable, it can happen that // the hidden type becomes the opaque type itself. In this case, this was an opaque @@ -149,25 +164,27 @@ impl<'tcx> RegionInferenceContext<'tcx> { // non-region parameters. This is necessary because within the new solver we perform // various query operations modulo regions, and thus could unsoundly select some impls // that don't hold. - if !ty.references_error() - && let Some((prev_decl_key, prev_span)) = decls_modulo_regions.insert( - infcx.tcx.erase_regions(opaque_type_key), - (opaque_type_key, concrete_type.span), - ) - && let Some((arg1, arg2)) = std::iter::zip( - prev_decl_key.iter_captured_args(infcx.tcx).map(|(_, arg)| arg), - opaque_type_key.iter_captured_args(infcx.tcx).map(|(_, arg)| arg), - ) - .find(|(arg1, arg2)| arg1 != arg2) + if let Some((prev_decl_key, prev_span)) = decls_modulo_regions.insert( + infcx.tcx.erase_regions(opaque_type_key), + (opaque_type_key, concrete_type.span), + ) && let Some((arg1, arg2)) = std::iter::zip( + prev_decl_key.iter_captured_args(infcx.tcx).map(|(_, arg)| arg), + opaque_type_key.iter_captured_args(infcx.tcx).map(|(_, arg)| arg), + ) + .find(|(arg1, arg2)| arg1 != arg2) { - infcx.dcx().emit_err(LifetimeMismatchOpaqueParam { - arg: arg1, - prev: arg2, - span: prev_span, - prev_span: concrete_type.span, - }); + errors.push(DeferredOpaqueTypeError::LifetimeMismatchOpaqueParam( + LifetimeMismatchOpaqueParam { + arg: arg1, + prev: arg2, + span: prev_span, + prev_span: concrete_type.span, + }, + )); } } + + errors } /// Map the regions in the type to named regions. This is similar to what @@ -260,19 +277,13 @@ impl<'tcx> InferCtxt<'tcx> { &self, opaque_type_key: OpaqueTypeKey<'tcx>, instantiated_ty: OpaqueHiddenType<'tcx>, - ) -> Ty<'tcx> { - if let Some(e) = self.tainted_by_errors() { - return Ty::new_error(self.tcx, e); - } - - if let Err(err) = check_opaque_type_parameter_valid( + ) -> Result, InvalidOpaqueTypeArgs<'tcx>> { + check_opaque_type_parameter_valid( self, opaque_type_key, instantiated_ty.span, DefiningScopeKind::MirBorrowck, - ) { - return Ty::new_error(self.tcx, err.report(self)); - } + )?; let definition_ty = instantiated_ty .remap_generic_params_to_declaration_params( @@ -282,10 +293,7 @@ impl<'tcx> InferCtxt<'tcx> { ) .ty; - if let Err(e) = definition_ty.error_reported() { - return Ty::new_error(self.tcx, e); - } - - definition_ty + definition_ty.error_reported()?; + Ok(definition_ty) } } diff --git a/tests/crashes/135528.rs b/tests/crashes/135528.rs deleted file mode 100644 index 171550e209e5c..0000000000000 --- a/tests/crashes/135528.rs +++ /dev/null @@ -1,20 +0,0 @@ -//@ known-bug: #135528 -//@ compile-flags: -Zvalidate-mir -Zinline-mir=yes -#![feature(type_alias_impl_trait)] -type Tait = impl Copy; - -fn set(x: &isize) -> isize { - *x -} - -#[define_opaque(Tait)] -fn d(x: Tait) { - set(x); -} - -#[define_opaque(Tait)] -fn other_define() -> Tait { - () -} - -fn main() {} diff --git a/tests/ui/impl-trait/issues/type-error-post-normalization-test.rs b/tests/ui/impl-trait/issues/type-error-post-normalization-test.rs new file mode 100644 index 0000000000000..0108bb236113d --- /dev/null +++ b/tests/ui/impl-trait/issues/type-error-post-normalization-test.rs @@ -0,0 +1,24 @@ +//@ compile-flags: -Zvalidate-mir -Zinline-mir=yes + +// This previously introduced a `{type_error}`` in the MIR body +// during the `PostAnalysisNormalize` pass. While the underlying issue +// #135528 did not get fixed, this reproducer no longer ICEs. + +#![feature(type_alias_impl_trait)] +type Tait = impl Copy; + +fn set(x: &isize) -> isize { + *x +} + +#[define_opaque(Tait)] +fn d(x: Tait) { + set(x); +} + +#[define_opaque(Tait)] +fn other_define() -> Tait { + () //~^ ERROR concrete type differs from previous defining opaque type use +} + +fn main() {} diff --git a/tests/ui/impl-trait/issues/type-error-post-normalization-test.stderr b/tests/ui/impl-trait/issues/type-error-post-normalization-test.stderr new file mode 100644 index 0000000000000..7d63c1cfbd6a2 --- /dev/null +++ b/tests/ui/impl-trait/issues/type-error-post-normalization-test.stderr @@ -0,0 +1,14 @@ +error: concrete type differs from previous defining opaque type use + --> $DIR/type-error-post-normalization-test.rs:20:22 + | +LL | fn other_define() -> Tait { + | ^^^^ expected `&isize`, got `()` + | +note: previous use here + --> $DIR/type-error-post-normalization-test.rs:16:9 + | +LL | set(x); + | ^ + +error: aborting due to 1 previous error + diff --git a/tests/ui/rfcs/type-alias-impl-trait/higher-ranked-regions-basic.rs b/tests/ui/rfcs/type-alias-impl-trait/higher-ranked-regions-basic.rs index 4cf2d1ac4a6be..80a4ab35527a1 100644 --- a/tests/ui/rfcs/type-alias-impl-trait/higher-ranked-regions-basic.rs +++ b/tests/ui/rfcs/type-alias-impl-trait/higher-ranked-regions-basic.rs @@ -41,6 +41,7 @@ mod capture_tait_complex_pass { #[define_opaque(Opq2)] fn test() -> Opq2 {} //~^ ERROR: expected generic lifetime parameter, found `'a` + //~| ERROR: expected generic lifetime parameter, found `'b` } // Same as the above, but make sure that different placeholder regions are not equal. @@ -80,6 +81,7 @@ mod constrain_pass { #[define_opaque(Opq2)] fn test() -> Opq2 {} //~^ ERROR: expected generic lifetime parameter, found `'a` + //~| ERROR: expected generic lifetime parameter, found `'a` } fn main() {} diff --git a/tests/ui/rfcs/type-alias-impl-trait/higher-ranked-regions-basic.stderr b/tests/ui/rfcs/type-alias-impl-trait/higher-ranked-regions-basic.stderr index 3614fc8f45c74..665b9a9169657 100644 --- a/tests/ui/rfcs/type-alias-impl-trait/higher-ranked-regions-basic.stderr +++ b/tests/ui/rfcs/type-alias-impl-trait/higher-ranked-regions-basic.stderr @@ -36,8 +36,17 @@ LL | type Opq1<'a> = impl for<'b> Trait<'b, Ty = Opq0<'b>>; // <- Note 'b LL | fn test() -> Opq2 {} | ^^ +error[E0792]: expected generic lifetime parameter, found `'b` + --> $DIR/higher-ranked-regions-basic.rs:42:23 + | +LL | type Opq0<'a> = impl Sized; + | -- this generic parameter must be used with a generic lifetime parameter +... +LL | fn test() -> Opq2 {} + | ^^ + error[E0700]: hidden type for `capture_tait_complex_fail::Opq0<'a>` captures lifetime that does not appear in bounds - --> $DIR/higher-ranked-regions-basic.rs:53:23 + --> $DIR/higher-ranked-regions-basic.rs:54:23 | LL | type Opq0<'a> = impl Sized; | ---------- opaque type defined here @@ -48,7 +57,7 @@ LL | fn test() -> Opq2 {} | ^^ error[E0792]: expected generic lifetime parameter, found `'a` - --> $DIR/higher-ranked-regions-basic.rs:62:65 + --> $DIR/higher-ranked-regions-basic.rs:63:65 | LL | type Opq0<'a, 'b> = impl Sized; | -- this generic parameter must be used with a generic lifetime parameter @@ -57,7 +66,7 @@ LL | fn test() -> impl for<'a> Trait<'a, Ty = Opq0<'a, 'static>> {} | ^^ error[E0792]: expected generic lifetime parameter, found `'a` - --> $DIR/higher-ranked-regions-basic.rs:71:60 + --> $DIR/higher-ranked-regions-basic.rs:72:60 | LL | type Opq0<'a, 'b> = impl Sized; | -- this generic parameter must be used with a generic lifetime parameter @@ -66,7 +75,7 @@ LL | fn test() -> impl for<'a> Trait<'a, Ty = Opq0<'a, 'a>> {} | ^^ error[E0792]: expected generic lifetime parameter, found `'a` - --> $DIR/higher-ranked-regions-basic.rs:81:23 + --> $DIR/higher-ranked-regions-basic.rs:82:23 | LL | type Opq1<'a> = impl for<'b> Trait<'b, Ty = Opq0<'a, 'b>>; | -- this generic parameter must be used with a generic lifetime parameter @@ -74,7 +83,16 @@ LL | type Opq1<'a> = impl for<'b> Trait<'b, Ty = Opq0<'a, 'b>>; LL | fn test() -> Opq2 {} | ^^ -error: aborting due to 8 previous errors +error[E0792]: expected generic lifetime parameter, found `'a` + --> $DIR/higher-ranked-regions-basic.rs:82:23 + | +LL | type Opq0<'a, 'b> = impl Sized; + | -- this generic parameter must be used with a generic lifetime parameter +... +LL | fn test() -> Opq2 {} + | ^^ + +error: aborting due to 10 previous errors Some errors have detailed explanations: E0700, E0792. For more information about an error, try `rustc --explain E0700`. diff --git a/tests/ui/type-alias-impl-trait/hkl_forbidden.rs b/tests/ui/type-alias-impl-trait/hkl_forbidden.rs index 994adc476e242..72da2af96b8d2 100644 --- a/tests/ui/type-alias-impl-trait/hkl_forbidden.rs +++ b/tests/ui/type-alias-impl-trait/hkl_forbidden.rs @@ -8,7 +8,9 @@ type Opaque<'a> = impl Sized + 'a; #[define_opaque(Opaque)] fn test(s: &str) -> (impl Fn(&str) -> Opaque<'_>, impl Fn(&str) -> Opaque<'_>) { - (id, id) //~ ERROR expected generic lifetime parameter, found `'_` + (id, id) + //~^ ERROR expected generic lifetime parameter, found `'_` + //~| ERROR expected generic lifetime parameter, found `'_` } fn id2<'a, 'b>(s: (&'a str, &'b str)) -> (&'a str, &'b str) { @@ -19,7 +21,9 @@ type Opaque2<'a> = impl Sized + 'a; #[define_opaque(Opaque2)] fn test2() -> impl for<'a, 'b> Fn((&'a str, &'b str)) -> (Opaque2<'a>, Opaque2<'b>) { - id2 //~ ERROR expected generic lifetime parameter, found `'a` + id2 + //~^ ERROR expected generic lifetime parameter, found `'a` + //~| ERROR expected generic lifetime parameter, found `'b` } type Opaque3<'a> = impl Sized + 'a; diff --git a/tests/ui/type-alias-impl-trait/hkl_forbidden.stderr b/tests/ui/type-alias-impl-trait/hkl_forbidden.stderr index d404d60f31ed9..315bdfb2272fc 100644 --- a/tests/ui/type-alias-impl-trait/hkl_forbidden.stderr +++ b/tests/ui/type-alias-impl-trait/hkl_forbidden.stderr @@ -7,8 +7,28 @@ LL | type Opaque<'a> = impl Sized + 'a; LL | (id, id) | ^^^^^^^^ +error[E0792]: expected generic lifetime parameter, found `'_` + --> $DIR/hkl_forbidden.rs:11:5 + | +LL | type Opaque<'a> = impl Sized + 'a; + | -- this generic parameter must be used with a generic lifetime parameter +... +LL | (id, id) + | ^^^^^^^^ + | + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` + error[E0792]: expected generic lifetime parameter, found `'a` - --> $DIR/hkl_forbidden.rs:22:5 + --> $DIR/hkl_forbidden.rs:24:5 + | +LL | type Opaque2<'a> = impl Sized + 'a; + | -- this generic parameter must be used with a generic lifetime parameter +... +LL | id2 + | ^^^ + +error[E0792]: expected generic lifetime parameter, found `'b` + --> $DIR/hkl_forbidden.rs:24:5 | LL | type Opaque2<'a> = impl Sized + 'a; | -- this generic parameter must be used with a generic lifetime parameter @@ -17,7 +37,7 @@ LL | id2 | ^^^ error[E0792]: expected generic lifetime parameter, found `'_` - --> $DIR/hkl_forbidden.rs:29:5 + --> $DIR/hkl_forbidden.rs:33:5 | LL | type Opaque3<'a> = impl Sized + 'a; | -- this generic parameter must be used with a generic lifetime parameter @@ -26,7 +46,7 @@ LL | (id, s) | ^^^^^^^ error[E0792]: expected generic lifetime parameter, found `'_` - --> $DIR/hkl_forbidden.rs:35:5 + --> $DIR/hkl_forbidden.rs:39:5 | LL | type Opaque4<'a> = impl Sized + 'a; | -- this generic parameter must be used with a generic lifetime parameter @@ -35,7 +55,7 @@ LL | (s, id) | ^^^^^^^ error[E0792]: expected generic lifetime parameter, found `'a` - --> $DIR/hkl_forbidden.rs:41:5 + --> $DIR/hkl_forbidden.rs:45:5 | LL | type Inner<'a> = impl Sized; | -- this generic parameter must be used with a generic lifetime parameter @@ -43,6 +63,6 @@ LL | type Inner<'a> = impl Sized; LL | |x| x | ^^^^^ -error: aborting due to 5 previous errors +error: aborting due to 7 previous errors For more information about this error, try `rustc --explain E0792`. diff --git a/tests/ui/type-alias-impl-trait/param_mismatch2.rs b/tests/ui/type-alias-impl-trait/param_mismatch2.rs index f6a9971141105..94c495cb33077 100644 --- a/tests/ui/type-alias-impl-trait/param_mismatch2.rs +++ b/tests/ui/type-alias-impl-trait/param_mismatch2.rs @@ -11,7 +11,9 @@ type Opaque<'a> = impl Sized + 'a; #[define_opaque(Opaque)] fn test(s: &str) -> (impl Fn(&str) -> Opaque<'_>, impl Fn(&str) -> Opaque<'_>) { - (id, id) //~ ERROR: expected generic lifetime parameter, found `'_` + (id, id) + //~^ ERROR: expected generic lifetime parameter, found `'_` + //~| ERROR: expected generic lifetime parameter, found `'_` } fn main() {} diff --git a/tests/ui/type-alias-impl-trait/param_mismatch2.stderr b/tests/ui/type-alias-impl-trait/param_mismatch2.stderr index f5ecade2f02e6..5c8a8af6501e9 100644 --- a/tests/ui/type-alias-impl-trait/param_mismatch2.stderr +++ b/tests/ui/type-alias-impl-trait/param_mismatch2.stderr @@ -7,6 +7,17 @@ LL | type Opaque<'a> = impl Sized + 'a; LL | (id, id) | ^^^^^^^^ -error: aborting due to 1 previous error +error[E0792]: expected generic lifetime parameter, found `'_` + --> $DIR/param_mismatch2.rs:14:5 + | +LL | type Opaque<'a> = impl Sized + 'a; + | -- this generic parameter must be used with a generic lifetime parameter +... +LL | (id, id) + | ^^^^^^^^ + | + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` + +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0792`. diff --git a/tests/ui/type-alias-impl-trait/param_mismatch3.rs b/tests/ui/type-alias-impl-trait/param_mismatch3.rs index 17b6f8bce2a81..1514dd66db93c 100644 --- a/tests/ui/type-alias-impl-trait/param_mismatch3.rs +++ b/tests/ui/type-alias-impl-trait/param_mismatch3.rs @@ -11,7 +11,9 @@ type Opaque<'a> = impl Sized + 'a; #[define_opaque(Opaque)] fn test() -> impl for<'a, 'b> Fn((&'a str, &'b str)) -> (Opaque<'a>, Opaque<'b>) { - id2 //~ ERROR expected generic lifetime parameter, found `'a` + id2 + //~^ ERROR expected generic lifetime parameter, found `'a` + //~| ERROR expected generic lifetime parameter, found `'b` } fn id(s: &str) -> &str { diff --git a/tests/ui/type-alias-impl-trait/param_mismatch3.stderr b/tests/ui/type-alias-impl-trait/param_mismatch3.stderr index 7565bbfc6ff0d..76be07ce38f5e 100644 --- a/tests/ui/type-alias-impl-trait/param_mismatch3.stderr +++ b/tests/ui/type-alias-impl-trait/param_mismatch3.stderr @@ -7,8 +7,17 @@ LL | type Opaque<'a> = impl Sized + 'a; LL | id2 | ^^^ +error[E0792]: expected generic lifetime parameter, found `'b` + --> $DIR/param_mismatch3.rs:14:5 + | +LL | type Opaque<'a> = impl Sized + 'a; + | -- this generic parameter must be used with a generic lifetime parameter +... +LL | id2 + | ^^^ + error[E0792]: expected generic lifetime parameter, found `'_` - --> $DIR/param_mismatch3.rs:25:5 + --> $DIR/param_mismatch3.rs:27:5 | LL | type Opaque2<'a> = impl Sized + 'a; | -- this generic parameter must be used with a generic lifetime parameter @@ -16,6 +25,6 @@ LL | type Opaque2<'a> = impl Sized + 'a; LL | (id, s) | ^^^^^^^ -error: aborting due to 2 previous errors +error: aborting due to 3 previous errors For more information about this error, try `rustc --explain E0792`. From c44fe70d0384d3d83d8f49f4e3045ed3e7e209b9 Mon Sep 17 00:00:00 2001 From: Makai Date: Sat, 9 Aug 2025 01:19:42 +0800 Subject: [PATCH 42/47] fix missing parenthesis in pretty discriminant --- compiler/rustc_public/src/mir/pretty.rs | 2 +- tests/ui/rustc_public-ir-print/async-closure.stdout | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_public/src/mir/pretty.rs b/compiler/rustc_public/src/mir/pretty.rs index 3183c020772a7..9dd1ce4de0ea6 100644 --- a/compiler/rustc_public/src/mir/pretty.rs +++ b/compiler/rustc_public/src/mir/pretty.rs @@ -100,7 +100,7 @@ fn pretty_statement(writer: &mut W, statement: &StatementKind) -> io:: writeln!(writer, "{INDENT}FakeRead({cause:?}, {place:?});") } StatementKind::SetDiscriminant { place, variant_index } => { - writeln!(writer, "{INDENT}discriminant({place:?} = {};", variant_index.to_index()) + writeln!(writer, "{INDENT}discriminant({place:?}) = {};", variant_index.to_index()) } StatementKind::Deinit(place) => writeln!(writer, "Deinit({place:?};"), StatementKind::StorageLive(local) => { diff --git a/tests/ui/rustc_public-ir-print/async-closure.stdout b/tests/ui/rustc_public-ir-print/async-closure.stdout index 4afb15af7a933..73e9b8fc097ab 100644 --- a/tests/ui/rustc_public-ir-print/async-closure.stdout +++ b/tests/ui/rustc_public-ir-print/async-closure.stdout @@ -63,7 +63,7 @@ fn foo::{closure#0}::{closure#0}(_1: Pin<&mut {async closure body@$DIR/async-clo StorageDead(_3); _0 = std::task::Poll::Ready(move _5); _10 = CopyForDeref((_1.0: &mut {async closure body@$DIR/async-closure.rs:9:22: 11:6})); - discriminant((*_10) = 1; + discriminant((*_10)) = 1; return; } bb2: { @@ -101,7 +101,7 @@ fn foo::{closure#0}::{synthetic#0}(_1: Pin<&mut {async closure body@$DIR/async-c StorageDead(_3); _0 = std::task::Poll::Ready(move _5); _10 = CopyForDeref((_1.0: &mut {async closure body@$DIR/async-closure.rs:9:22: 11:6})); - discriminant((*_10) = 1; + discriminant((*_10)) = 1; return; } bb2: { From b2d524c43d113bba72e0a23b4a97cbbeb6eb39bb Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 8 Aug 2025 18:46:02 +0000 Subject: [PATCH 43/47] Recover for PAT = EXPR {} --- compiler/rustc_parse/messages.ftl | 2 +- compiler/rustc_parse/src/errors.rs | 11 ++++---- compiler/rustc_parse/src/parser/expr.rs | 2 ++ .../ui/suggestions/for-loop-missing-in.fixed | 7 +++-- tests/ui/suggestions/for-loop-missing-in.rs | 7 +++-- .../ui/suggestions/for-loop-missing-in.stderr | 26 ++++++++++++++----- 6 files changed, 33 insertions(+), 22 deletions(-) diff --git a/compiler/rustc_parse/messages.ftl b/compiler/rustc_parse/messages.ftl index 7059ffbf375f7..aaf1b6c05bf4a 100644 --- a/compiler/rustc_parse/messages.ftl +++ b/compiler/rustc_parse/messages.ftl @@ -642,7 +642,7 @@ parse_missing_for_in_trait_impl = missing `for` in a trait impl .suggestion = add `for` here parse_missing_in_in_for_loop = missing `in` in `for` loop - .use_in_not_of = try using `in` here instead + .use_in = try using `in` here instead .add_in = try adding `in` here parse_missing_let_before_mut = missing keyword diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index 48ff0394d463b..ddb2c545c787e 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -585,14 +585,13 @@ pub(crate) struct MissingInInForLoop { #[derive(Subdiagnostic)] pub(crate) enum MissingInInForLoopSub { + // User wrote `for pat of expr {}` // Has been misleading, at least in the past (closed Issue #48492), thus maybe-incorrect - #[suggestion( - parse_use_in_not_of, - style = "verbose", - applicability = "maybe-incorrect", - code = "in" - )] + #[suggestion(parse_use_in, style = "verbose", applicability = "maybe-incorrect", code = "in")] InNotOf(#[primary_span] Span), + // User wrote `for pat = expr {}` + #[suggestion(parse_use_in, style = "verbose", applicability = "maybe-incorrect", code = "in")] + InNotEq(#[primary_span] Span), #[suggestion(parse_add_in, style = "verbose", applicability = "maybe-incorrect", code = " in ")] AddIn(#[primary_span] Span), } diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 35b987cf50fa2..cbf6b78431cb1 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -3028,6 +3028,8 @@ impl<'a> Parser<'a> { let span = self.token.span; self.bump(); (span, errors::MissingInInForLoopSub::InNotOf) + } else if self.eat(exp!(Eq)) { + (self.prev_token.span, errors::MissingInInForLoopSub::InNotEq) } else { (self.prev_token.span.between(self.token.span), errors::MissingInInForLoopSub::AddIn) }; diff --git a/tests/ui/suggestions/for-loop-missing-in.fixed b/tests/ui/suggestions/for-loop-missing-in.fixed index ff376b5a52c5f..396c3ff87fffa 100644 --- a/tests/ui/suggestions/for-loop-missing-in.fixed +++ b/tests/ui/suggestions/for-loop-missing-in.fixed @@ -1,8 +1,7 @@ //@ run-rustfix fn main() { - for _i in 0..2 { //~ ERROR missing `in` - } - for _i in 0..2 { //~ ERROR missing `in` - } + for _i in 0..2 {} //~ ERROR missing `in` + for _i in 0..2 {} //~ ERROR missing `in` + for _i in 0..2 {} //~ ERROR missing `in` } diff --git a/tests/ui/suggestions/for-loop-missing-in.rs b/tests/ui/suggestions/for-loop-missing-in.rs index 28c12aec665ad..6c2092106cb2d 100644 --- a/tests/ui/suggestions/for-loop-missing-in.rs +++ b/tests/ui/suggestions/for-loop-missing-in.rs @@ -1,8 +1,7 @@ //@ run-rustfix fn main() { - for _i 0..2 { //~ ERROR missing `in` - } - for _i of 0..2 { //~ ERROR missing `in` - } + for _i 0..2 {} //~ ERROR missing `in` + for _i of 0..2 {} //~ ERROR missing `in` + for _i = 0..2 {} //~ ERROR missing `in` } diff --git a/tests/ui/suggestions/for-loop-missing-in.stderr b/tests/ui/suggestions/for-loop-missing-in.stderr index 4e0cb229d50a2..b9c8c39340681 100644 --- a/tests/ui/suggestions/for-loop-missing-in.stderr +++ b/tests/ui/suggestions/for-loop-missing-in.stderr @@ -1,25 +1,37 @@ error: missing `in` in `for` loop --> $DIR/for-loop-missing-in.rs:4:11 | -LL | for _i 0..2 { +LL | for _i 0..2 {} | ^ | help: try adding `in` here | -LL | for _i in 0..2 { +LL | for _i in 0..2 {} | ++ error: missing `in` in `for` loop - --> $DIR/for-loop-missing-in.rs:6:12 + --> $DIR/for-loop-missing-in.rs:5:12 | -LL | for _i of 0..2 { +LL | for _i of 0..2 {} | ^^ | help: try using `in` here instead | -LL - for _i of 0..2 { -LL + for _i in 0..2 { +LL - for _i of 0..2 {} +LL + for _i in 0..2 {} + | + +error: missing `in` in `for` loop + --> $DIR/for-loop-missing-in.rs:6:12 + | +LL | for _i = 0..2 {} + | ^ + | +help: try using `in` here instead + | +LL - for _i = 0..2 {} +LL + for _i in 0..2 {} | -error: aborting due to 2 previous errors +error: aborting due to 3 previous errors From 7670cdeb709b33cb7d57a7e2fdb790f50cbe6858 Mon Sep 17 00:00:00 2001 From: Cameron Steffen Date: Thu, 7 Aug 2025 14:10:59 -0500 Subject: [PATCH 44/47] Refactor map_unit_fn lint --- compiler/rustc_lint/src/lints.rs | 3 +- compiler/rustc_lint/src/map_unit_fn.rs | 103 +++++++++---------------- compiler/rustc_span/src/symbol.rs | 1 + 3 files changed, 39 insertions(+), 68 deletions(-) diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index ac6147b163125..73e69a1791a4d 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -902,9 +902,8 @@ pub(crate) struct MappingToUnit { pub argument_label: Span, #[label(lint_map_label)] pub map_label: Span, - #[suggestion(style = "verbose", code = "{replace}", applicability = "maybe-incorrect")] + #[suggestion(style = "verbose", code = "for_each", applicability = "maybe-incorrect")] pub suggestion: Span, - pub replace: String, } // internal.rs diff --git a/compiler/rustc_lint/src/map_unit_fn.rs b/compiler/rustc_lint/src/map_unit_fn.rs index 34210137bde18..18a947dc1ee06 100644 --- a/compiler/rustc_lint/src/map_unit_fn.rs +++ b/compiler/rustc_lint/src/map_unit_fn.rs @@ -1,6 +1,7 @@ -use rustc_hir::{Expr, ExprKind, HirId, Stmt, StmtKind}; -use rustc_middle::ty::{self, Ty}; +use rustc_hir::{Expr, ExprKind, Stmt, StmtKind}; +use rustc_middle::ty::{self}; use rustc_session::{declare_lint, declare_lint_pass}; +use rustc_span::sym; use crate::lints::MappingToUnit; use crate::{LateContext, LateLintPass, LintContext}; @@ -39,58 +40,43 @@ declare_lint_pass!(MapUnitFn => [MAP_UNIT_FN]); impl<'tcx> LateLintPass<'tcx> for MapUnitFn { fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &Stmt<'_>) { - if stmt.span.from_expansion() { + let StmtKind::Semi(expr) = stmt.kind else { return; - } - - if let StmtKind::Semi(expr) = stmt.kind - && let ExprKind::MethodCall(path, receiver, args, span) = expr.kind + }; + let ExprKind::MethodCall(path, receiver, [arg], span) = expr.kind else { + return; + }; + if path.ident.name != sym::map + || stmt.span.from_expansion() + || receiver.span.from_expansion() + || arg.span.from_expansion() + || !is_impl_slice(cx, receiver) + || !cx + .typeck_results() + .type_dependent_def_id(expr.hir_id) + .is_some_and(|id| cx.tcx.is_diagnostic_item(sym::IteratorMap, id)) { - if path.ident.name.as_str() == "map" { - if receiver.span.from_expansion() - || args.iter().any(|e| e.span.from_expansion()) - || !is_impl_slice(cx, receiver) - || !is_diagnostic_name(cx, expr.hir_id, "IteratorMap") - { - return; - } - let arg_ty = cx.typeck_results().expr_ty(&args[0]); - let default_span = args[0].span; - if let ty::FnDef(id, _) = arg_ty.kind() { - let fn_ty = cx.tcx.fn_sig(id).skip_binder(); - let ret_ty = fn_ty.output().skip_binder(); - if is_unit_type(ret_ty) { - cx.emit_span_lint( - MAP_UNIT_FN, - span, - MappingToUnit { - function_label: cx.tcx.span_of_impl(*id).unwrap_or(default_span), - argument_label: args[0].span, - map_label: span, - suggestion: path.ident.span, - replace: "for_each".to_string(), - }, - ) - } - } else if let ty::Closure(id, subs) = arg_ty.kind() { - let cl_ty = subs.as_closure().sig(); - let ret_ty = cl_ty.output().skip_binder(); - if is_unit_type(ret_ty) { - cx.emit_span_lint( - MAP_UNIT_FN, - span, - MappingToUnit { - function_label: cx.tcx.span_of_impl(*id).unwrap_or(default_span), - argument_label: args[0].span, - map_label: span, - suggestion: path.ident.span, - replace: "for_each".to_string(), - }, - ) - } - } - } + return; } + let (id, sig) = match *cx.typeck_results().expr_ty(arg).kind() { + ty::Closure(id, subs) => (id, subs.as_closure().sig()), + ty::FnDef(id, _) => (id, cx.tcx.fn_sig(id).skip_binder()), + _ => return, + }; + let ret_ty = sig.output().skip_binder(); + if !(ret_ty.is_unit() || ret_ty.is_never()) { + return; + } + cx.emit_span_lint( + MAP_UNIT_FN, + span, + MappingToUnit { + function_label: cx.tcx.span_of_impl(id).unwrap_or(arg.span), + argument_label: arg.span, + map_label: span, + suggestion: path.ident.span, + }, + ); } } @@ -102,18 +88,3 @@ fn is_impl_slice(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { } false } - -fn is_unit_type(ty: Ty<'_>) -> bool { - ty.is_unit() || ty.is_never() -} - -fn is_diagnostic_name(cx: &LateContext<'_>, id: HirId, name: &str) -> bool { - if let Some(def_id) = cx.typeck_results().type_dependent_def_id(id) - && let Some(item) = cx.tcx.get_diagnostic_name(def_id) - { - if item.as_str() == name { - return true; - } - } - false -} diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 36197950221ee..bb3a2e1526a9a 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -280,6 +280,7 @@ symbols! { IterPeekable, Iterator, IteratorItem, + IteratorMap, Layout, Left, LinkedList, From eec8585f656e853901ff6c1106d1f15456bfac41 Mon Sep 17 00:00:00 2001 From: Cameron Steffen Date: Fri, 8 Aug 2025 17:01:57 -0500 Subject: [PATCH 45/47] Reduce indirect assoc parent queries --- compiler/rustc_hir_analysis/src/collect/predicates_of.rs | 3 +-- compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs | 2 +- compiler/rustc_lint/src/default_could_be_derived.rs | 3 +-- .../src/error_reporting/infer/note_and_explain.rs | 3 +-- src/librustdoc/clean/mod.rs | 2 +- 5 files changed, 5 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs index 522409a5ee52f..8dd13da4fa7bc 100644 --- a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs @@ -517,8 +517,7 @@ pub(super) fn explicit_predicates_of<'tcx>( projection.args == trait_identity_args // FIXME(return_type_notation): This check should be more robust && !tcx.is_impl_trait_in_trait(projection.def_id) - && tcx.associated_item(projection.def_id).container_id(tcx) - == def_id.to_def_id() + && tcx.parent(projection.def_id) == def_id.to_def_id() } else { false } diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs index 3e446b7c656f1..93b82acf6212c 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs @@ -755,7 +755,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { let limit = if candidates.len() == 5 { 5 } else { 4 }; for (index, &item) in candidates.iter().take(limit).enumerate() { - let impl_ = tcx.impl_of_assoc(item).unwrap(); + let impl_ = tcx.parent(item); let note_span = if item.is_local() { Some(tcx.def_span(item)) diff --git a/compiler/rustc_lint/src/default_could_be_derived.rs b/compiler/rustc_lint/src/default_could_be_derived.rs index 7c39d8917cef3..1d92cfbc03916 100644 --- a/compiler/rustc_lint/src/default_could_be_derived.rs +++ b/compiler/rustc_lint/src/default_could_be_derived.rs @@ -62,8 +62,7 @@ impl<'tcx> LateLintPass<'tcx> for DefaultCouldBeDerived { // Look for manual implementations of `Default`. let Some(default_def_id) = cx.tcx.get_diagnostic_item(sym::Default) else { return }; let hir::ImplItemKind::Fn(_sig, body_id) = impl_item.kind else { return }; - let assoc = cx.tcx.associated_item(impl_item.owner_id); - let parent = assoc.container_id(cx.tcx); + let parent = cx.tcx.parent(impl_item.owner_id.to_def_id()); if find_attr!(cx.tcx.get_all_attrs(parent), AttributeKind::AutomaticallyDerived(..)) { // We don't care about what `#[derive(Default)]` produces in this lint. return; diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs index 129d0963a75a8..8f0f6d0bf26a3 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs @@ -653,7 +653,6 @@ impl Trait for X { ) ); let impl_comparison = matches!(cause_code, ObligationCauseCode::CompareImplItem { .. }); - let assoc = tcx.associated_item(proj_ty.def_id); if impl_comparison { // We do not want to suggest calling functions when the reason of the // type error is a comparison of an `impl` with its `trait`. @@ -661,7 +660,7 @@ impl Trait for X { let point_at_assoc_fn = if callable_scope && self.point_at_methods_that_satisfy_associated_type( diag, - assoc.container_id(tcx), + tcx.parent(proj_ty.def_id), current_method_ident, proj_ty.def_id, values.expected, diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 743ed2b504536..7194c2fcededa 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1662,7 +1662,7 @@ fn clean_qpath<'tcx>(hir_ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> Type } let trait_segments = &p.segments[..p.segments.len() - 1]; - let trait_def = cx.tcx.associated_item(p.res.def_id()).container_id(cx.tcx); + let trait_def = cx.tcx.parent(p.res.def_id()); let trait_ = self::Path { res: Res::Def(DefKind::Trait, trait_def), segments: trait_segments.iter().map(|x| clean_path_segment(x, cx)).collect(), From b39357bbf3fc4688793a6a4671b509efde8834ce Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 8 Aug 2025 14:04:47 +0200 Subject: [PATCH 46/47] Rank doc aliases lower than equivalently matched items --- src/librustdoc/html/static/js/search.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 2caf214ff73d4..505652c0f4a7c 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -3340,6 +3340,13 @@ class DocSearch { return a - b; } + // sort doc alias items later + a = Number(aaa.item.is_alias === true); + b = Number(bbb.item.is_alias === true); + if (a !== b) { + return a - b; + } + // sort by item name (lexicographically larger goes later) let aw = aaa.word; let bw = bbb.word; From a34bd2baf586b4a6284b11852fede24ad557f787 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 8 Aug 2025 14:05:19 +0200 Subject: [PATCH 47/47] Add regression test for doc alias matching vs equivalently matched items --- .../rustdoc-js/doc-alias-after-other-items.js | 38 +++++++++++++++++++ .../rustdoc-js/doc-alias-after-other-items.rs | 9 +++++ 2 files changed, 47 insertions(+) create mode 100644 tests/rustdoc-js/doc-alias-after-other-items.js create mode 100644 tests/rustdoc-js/doc-alias-after-other-items.rs diff --git a/tests/rustdoc-js/doc-alias-after-other-items.js b/tests/rustdoc-js/doc-alias-after-other-items.js new file mode 100644 index 0000000000000..5b22ef6769825 --- /dev/null +++ b/tests/rustdoc-js/doc-alias-after-other-items.js @@ -0,0 +1,38 @@ +// exact-check + +// Checking that doc aliases are always listed after items with equivalent matching. + +const EXPECTED = [ + { + 'query': 'coo', + 'others': [ + { + 'path': 'doc_alias_after_other_items', + 'name': 'Foo', + 'href': '../doc_alias_after_other_items/struct.Foo.html', + }, + { + 'path': 'doc_alias_after_other_items', + 'name': 'bar', + 'alias': 'Boo', + 'is_alias': true + }, + ], + }, + { + 'query': '"confiture"', + 'others': [ + { + 'path': 'doc_alias_after_other_items', + 'name': 'Confiture', + 'href': '../doc_alias_after_other_items/struct.Confiture.html', + }, + { + 'path': 'doc_alias_after_other_items', + 'name': 'this_is_a_long_name', + 'alias': 'Confiture', + 'is_alias': true + }, + ], + }, +]; diff --git a/tests/rustdoc-js/doc-alias-after-other-items.rs b/tests/rustdoc-js/doc-alias-after-other-items.rs new file mode 100644 index 0000000000000..2ed555c8e2f37 --- /dev/null +++ b/tests/rustdoc-js/doc-alias-after-other-items.rs @@ -0,0 +1,9 @@ +pub struct Foo; + +#[doc(alias = "Boo")] +pub fn bar() {} + +pub struct Confiture; + +#[doc(alias = "Confiture")] +pub fn this_is_a_long_name() {}