From 25b81024a50647cc364cc1a7ab6aa000fef4b915 Mon Sep 17 00:00:00 2001 From: Joshua Liebow-Feeser Date: Thu, 7 Sep 2023 12:54:34 -0700 Subject: [PATCH 1/8] Guarantee that Layout::align returns a non-zero power of two --- library/core/src/alloc/layout.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/core/src/alloc/layout.rs b/library/core/src/alloc/layout.rs index 59730303734..dcfb42759f3 100644 --- a/library/core/src/alloc/layout.rs +++ b/library/core/src/alloc/layout.rs @@ -130,6 +130,8 @@ impl Layout { } /// The minimum byte alignment for a memory block of this layout. + /// + /// The returned alignment is guaranteed to be a non-zero power of two. #[stable(feature = "alloc_layout", since = "1.28.0")] #[rustc_const_stable(feature = "const_alloc_layout_size_align", since = "1.50.0")] #[must_use = "this returns the minimum alignment, \ From 86e9b0f0d4881787bed2ff77d3bb6db56d077ee2 Mon Sep 17 00:00:00 2001 From: onur-ozkan Date: Mon, 9 Oct 2023 21:43:33 +0300 Subject: [PATCH 2/8] add `SAFETY` block on usage of unsafe `getuid` Signed-off-by: onur-ozkan --- src/bootstrap/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/bootstrap/lib.rs b/src/bootstrap/lib.rs index 5c78015e560..0ac856479f8 100644 --- a/src/bootstrap/lib.rs +++ b/src/bootstrap/lib.rs @@ -368,6 +368,10 @@ impl Build { // https://github.com/rust-lang/rust/blob/a8a33cf27166d3eabaffc58ed3799e054af3b0c6/src/bootstrap/bootstrap.py#L796-L797 let is_sudo = match env::var_os("SUDO_USER") { Some(_sudo_user) => { + // SAFETY: getuid() system call is always successful and no return value is reserved + // to indicate an error. + // + // For more context, see https://man7.org/linux/man-pages/man2/geteuid.2.html let uid = unsafe { libc::getuid() }; uid == 0 } From 6efc71c9205f4d02d2ffb5a436408a90d3a52c53 Mon Sep 17 00:00:00 2001 From: Chris Wailes Date: Tue, 10 Oct 2023 16:26:26 -0700 Subject: [PATCH 3/8] Add the V (vector) extension to the riscv64-linux-android target spec This feature has been enabled and tested internally in the Android project. --- compiler/rustc_target/src/spec/riscv64_linux_android.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_target/src/spec/riscv64_linux_android.rs b/compiler/rustc_target/src/spec/riscv64_linux_android.rs index 91f5e562d8b..5312c761d6f 100644 --- a/compiler/rustc_target/src/spec/riscv64_linux_android.rs +++ b/compiler/rustc_target/src/spec/riscv64_linux_android.rs @@ -9,7 +9,7 @@ pub fn target() -> Target { options: TargetOptions { code_model: Some(CodeModel::Medium), cpu: "generic-rv64".into(), - features: "+m,+a,+f,+d,+c,+Zba,+Zbb,+Zbs".into(), + features: "+m,+a,+f,+d,+c,+Zba,+Zbb,+Zbs,+V".into(), llvm_abiname: "lp64d".into(), supported_sanitizers: SanitizerSet::ADDRESS, max_atomic_width: Some(64), From 899f81fcb8a4ead2524d5728cb40518b085496bf Mon Sep 17 00:00:00 2001 From: Chris Wailes Date: Wed, 11 Oct 2023 17:01:39 -0700 Subject: [PATCH 4/8] Add documentation for the riscv64-android-linux target This commit adds additional documentation describing the features/extensions required by the riscv64-linux-android target. --- src/doc/rustc/src/platform-support/android.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/doc/rustc/src/platform-support/android.md b/src/doc/rustc/src/platform-support/android.md index 4ef74295e0f..9ddf00e3a50 100644 --- a/src/doc/rustc/src/platform-support/android.md +++ b/src/doc/rustc/src/platform-support/android.md @@ -45,3 +45,19 @@ The riscv64-linux-android target is supported as a Tier 3 target. A list of all supported targets can be found [here](../platform-support.html) + +## Architecture Notes + +### riscv64-linux-android + +Currently the `riscv64-linux-android` target requires the following architecture features/extensions: + +* `a` (atomics) +* `d` (double-precision floating-point) +* `c` (compressed instruction set) +* `f` (single-precision floating-point) +* `m` (multiplication and division) +* `v` (vector) +* `Zba` (address calculation instructions) +* `Zbb` (base instructions) +* `Zbs` (single-bit instructions) From a9b0966aa5593cf3419aae255b7ca8a85509e33e Mon Sep 17 00:00:00 2001 From: Joshua Liebow-Feeser Date: Thu, 12 Oct 2023 16:03:45 -0700 Subject: [PATCH 5/8] Update library/core/src/alloc/layout.rs Co-authored-by: David Tolnay --- library/core/src/alloc/layout.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/alloc/layout.rs b/library/core/src/alloc/layout.rs index dcfb42759f3..65946e09ff9 100644 --- a/library/core/src/alloc/layout.rs +++ b/library/core/src/alloc/layout.rs @@ -131,7 +131,7 @@ impl Layout { /// The minimum byte alignment for a memory block of this layout. /// - /// The returned alignment is guaranteed to be a non-zero power of two. + /// The returned alignment is guaranteed to be a power of two. #[stable(feature = "alloc_layout", since = "1.28.0")] #[rustc_const_stable(feature = "const_alloc_layout_size_align", since = "1.50.0")] #[must_use = "this returns the minimum alignment, \ From 1bc6ae44014f953844a61123b7984f39b683ad78 Mon Sep 17 00:00:00 2001 From: lcnr Date: Fri, 13 Oct 2023 09:42:51 +0000 Subject: [PATCH 6/8] explicitly handle auto trait leakage in coherence --- .../src/solve/trait_goals.rs | 5 +-- .../src/traits/select/candidate_assembly.rs | 33 +++++++++++-------- ...tderr => auto-trait-coherence.next.stderr} | 2 +- .../auto-trait-coherence.old.stderr | 12 +++++++ ...{auto-trait.rs => auto-trait-coherence.rs} | 3 ++ 5 files changed, 38 insertions(+), 17 deletions(-) rename tests/ui/impl-trait/{auto-trait.stderr => auto-trait-coherence.next.stderr} (91%) create mode 100644 tests/ui/impl-trait/auto-trait-coherence.old.stderr rename tests/ui/impl-trait/{auto-trait.rs => auto-trait-coherence.rs} (90%) diff --git a/compiler/rustc_trait_selection/src/solve/trait_goals.rs b/compiler/rustc_trait_selection/src/solve/trait_goals.rs index 8055c63b9f3..d87e05ad02f 100644 --- a/compiler/rustc_trait_selection/src/solve/trait_goals.rs +++ b/compiler/rustc_trait_selection/src/solve/trait_goals.rs @@ -136,12 +136,13 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> { // `assemble_candidates_after_normalizing_self_ty`, and we'd // just be registering an identical candidate here. // - // Returning `Err(NoSolution)` here is ok in `SolverMode::Coherence` - // since we'll always be registering an ambiguous candidate in + // We always return `Err(NoSolution)` here in `SolverMode::Coherence` + // since we'll always register an ambiguous candidate in // `assemble_candidates_after_normalizing_self_ty` due to normalizing // the TAIT. if let ty::Alias(ty::Opaque, opaque_ty) = goal.predicate.self_ty().kind() { if matches!(goal.param_env.reveal(), Reveal::All) + || matches!(ecx.solver_mode(), SolverMode::Coherence) || opaque_ty .def_id .as_local() diff --git a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs index bead8758ad6..f08cf6cef5b 100644 --- a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs +++ b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs @@ -492,7 +492,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { // this trait and type. } ty::Param(..) - | ty::Alias(ty::Projection | ty::Inherent, ..) + | ty::Alias(ty::Projection | ty::Inherent | ty::Weak, ..) | ty::Placeholder(..) | ty::Bound(..) => { // In these cases, we don't know what the actual @@ -536,20 +536,25 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { ); } - ty::Alias(_, _) - if candidates.vec.iter().any(|c| matches!(c, ProjectionCandidate(..))) => - { - // We do not generate an auto impl candidate for `impl Trait`s which already - // reference our auto trait. - // - // For example during candidate assembly for `impl Send: Send`, we don't have - // to look at the constituent types for this opaque types to figure out that this - // trivially holds. - // - // Note that this is only sound as projection candidates of opaque types - // are always applicable for auto traits. + ty::Alias(ty::Opaque, _) => { + if candidates.vec.iter().any(|c| matches!(c, ProjectionCandidate(..))) { + // We do not generate an auto impl candidate for `impl Trait`s which already + // reference our auto trait. + // + // For example during candidate assembly for `impl Send: Send`, we don't have + // to look at the constituent types for this opaque types to figure out that this + // trivially holds. + // + // Note that this is only sound as projection candidates of opaque types + // are always applicable for auto traits. + } else if self.infcx.intercrate { + // We do not emit auto trait candidates for opaque types in coherence. + // Doing so can result in weird dependency cycles. + candidates.ambiguous = true; + } else { + candidates.vec.push(AutoImplCandidate) + } } - ty::Alias(_, _) => candidates.vec.push(AutoImplCandidate), ty::Bool | ty::Char diff --git a/tests/ui/impl-trait/auto-trait.stderr b/tests/ui/impl-trait/auto-trait-coherence.next.stderr similarity index 91% rename from tests/ui/impl-trait/auto-trait.stderr rename to tests/ui/impl-trait/auto-trait-coherence.next.stderr index 81009413c9a..7833ac688ba 100644 --- a/tests/ui/impl-trait/auto-trait.stderr +++ b/tests/ui/impl-trait/auto-trait-coherence.next.stderr @@ -1,5 +1,5 @@ error[E0119]: conflicting implementations of trait `AnotherTrait` for type `D` - --> $DIR/auto-trait.rs:21:1 + --> $DIR/auto-trait-coherence.rs:24:1 | LL | impl AnotherTrait for T {} | -------------------------------- first implementation here diff --git a/tests/ui/impl-trait/auto-trait-coherence.old.stderr b/tests/ui/impl-trait/auto-trait-coherence.old.stderr new file mode 100644 index 00000000000..7833ac688ba --- /dev/null +++ b/tests/ui/impl-trait/auto-trait-coherence.old.stderr @@ -0,0 +1,12 @@ +error[E0119]: conflicting implementations of trait `AnotherTrait` for type `D` + --> $DIR/auto-trait-coherence.rs:24:1 + | +LL | impl AnotherTrait for T {} + | -------------------------------- first implementation here +... +LL | impl AnotherTrait for D { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `D` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0119`. diff --git a/tests/ui/impl-trait/auto-trait.rs b/tests/ui/impl-trait/auto-trait-coherence.rs similarity index 90% rename from tests/ui/impl-trait/auto-trait.rs rename to tests/ui/impl-trait/auto-trait-coherence.rs index 35994e4a5ba..a5cd01a87ff 100644 --- a/tests/ui/impl-trait/auto-trait.rs +++ b/tests/ui/impl-trait/auto-trait-coherence.rs @@ -1,3 +1,6 @@ +// revisions: old next +//[next] compile-flags: -Ztrait-solver=next + // Tests that type alias impls traits do not leak auto-traits for // the purposes of coherence checking #![feature(type_alias_impl_trait)] From 166c3534849c88e38ca730457970d916a9d31f1a Mon Sep 17 00:00:00 2001 From: Chris Wailes Date: Fri, 13 Oct 2023 10:01:14 -0700 Subject: [PATCH 7/8] Lowercase the feature flags for riscv64-linux-android --- compiler/rustc_target/src/spec/riscv64_linux_android.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_target/src/spec/riscv64_linux_android.rs b/compiler/rustc_target/src/spec/riscv64_linux_android.rs index 5312c761d6f..121237f6ba4 100644 --- a/compiler/rustc_target/src/spec/riscv64_linux_android.rs +++ b/compiler/rustc_target/src/spec/riscv64_linux_android.rs @@ -9,7 +9,7 @@ pub fn target() -> Target { options: TargetOptions { code_model: Some(CodeModel::Medium), cpu: "generic-rv64".into(), - features: "+m,+a,+f,+d,+c,+Zba,+Zbb,+Zbs,+V".into(), + features: "+m,+a,+f,+d,+c,+zba,+zbb,+zbs,+v".into(), llvm_abiname: "lp64d".into(), supported_sanitizers: SanitizerSet::ADDRESS, max_atomic_width: Some(64), From feedd68f80e15616511493feda4c9c60d5571e73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 13 Oct 2023 00:28:49 +0000 Subject: [PATCH 8/8] Remove some unnecessary `unwrap`s --- .../rustc_hir_typeck/src/fn_ctxt/suggestions.rs | 8 ++++++-- compiler/rustc_hir_typeck/src/method/suggest.rs | 15 +++++++++------ compiler/rustc_lint/src/unused.rs | 2 +- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index 9999fa2e59c..45153699794 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -604,8 +604,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return false; } let box_found = Ty::new_box(self.tcx, found); - let pin_box_found = Ty::new_lang_item(self.tcx, box_found, LangItem::Pin).unwrap(); - let pin_found = Ty::new_lang_item(self.tcx, found, LangItem::Pin).unwrap(); + let Some(pin_box_found) = Ty::new_lang_item(self.tcx, box_found, LangItem::Pin) else { + return false; + }; + let Some(pin_found) = Ty::new_lang_item(self.tcx, found, LangItem::Pin) else { + return false; + }; match expected.kind() { ty::Adt(def, _) if Some(def.did()) == pin_did => { if self.can_coerce(pin_box_found, expected) { diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index a4bbb16026a..bda1df4b753 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -1616,7 +1616,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { continue; } - let range_def_id = self.tcx.require_lang_item(lang_item.unwrap(), None); + let Some(range_def_id) = + lang_item.and_then(|lang_item| self.tcx.lang_items().get(lang_item)) + else { + continue; + }; let range_ty = self.tcx.type_of(range_def_id).instantiate(self.tcx, &[actual.into()]); @@ -2539,11 +2543,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { Err(_) => (), } - let pred = ty::TraitRef::new( - self.tcx, - self.tcx.lang_items().unpin_trait().unwrap(), - [*rcvr_ty], - ); + let Some(unpin_trait) = self.tcx.lang_items().unpin_trait() else { + return; + }; + let pred = ty::TraitRef::new(self.tcx, unpin_trait, [*rcvr_ty]); let unpin = self.predicate_must_hold_considering_regions(&Obligation::new( self.tcx, ObligationCause::misc(rcvr.span, self.body_id), diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs index d5beff4f101..1e7a839e931 100644 --- a/compiler/rustc_lint/src/unused.rs +++ b/compiler/rustc_lint/src/unused.rs @@ -353,7 +353,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults { ty::Generator(def_id, ..) => { // async fn should be treated as "implementor of `Future`" let must_use = if cx.tcx.generator_is_async(def_id) { - let def_id = cx.tcx.lang_items().future_trait().unwrap(); + let def_id = cx.tcx.lang_items().future_trait()?; is_def_must_use(cx, def_id, span) .map(|inner| MustUsePath::Opaque(Box::new(inner))) } else {