From 40f5e6899d00484f05a3f1b227180a69a43bea0f Mon Sep 17 00:00:00 2001 From: Tyler Mandry Date: Thu, 28 Dec 2023 16:20:02 -0800 Subject: [PATCH 1/8] Build Fuchsia on 8 cores instead of 16 --- .github/workflows/ci.yml | 2 +- src/ci/github-actions/ci.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index caf97abf78d9e..e8e103df68b3b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -291,7 +291,7 @@ jobs: - name: x86_64-gnu-integration env: CI_ONLY_WHEN_CHANNEL: nightly - os: ubuntu-20.04-16core-64gb + os: ubuntu-20.04-8core-32gb - name: x86_64-gnu-debug os: ubuntu-20.04-8core-32gb env: {} diff --git a/src/ci/github-actions/ci.yml b/src/ci/github-actions/ci.yml index 68a3afc910f22..659d048cc1d9a 100644 --- a/src/ci/github-actions/ci.yml +++ b/src/ci/github-actions/ci.yml @@ -476,7 +476,7 @@ jobs: # nightly features to compile, and this job would fail if # executed on beta and stable. CI_ONLY_WHEN_CHANNEL: nightly - <<: *job-linux-16c + <<: *job-linux-8c - name: x86_64-gnu-debug <<: *job-linux-8c From 53bf511af2b9b54702943eb4fe6e4f17d9fc96c6 Mon Sep 17 00:00:00 2001 From: Tyler Mandry Date: Thu, 28 Dec 2023 16:44:49 -0800 Subject: [PATCH 2/8] Skip building cranelift for Fuchsia This refactors run.sh to never override an explicit $CODEGEN_BACKENDS if set in the build. --- .../docker/host-x86_64/x86_64-gnu-integration/Dockerfile | 3 +++ src/ci/run.sh | 8 +++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/ci/docker/host-x86_64/x86_64-gnu-integration/Dockerfile b/src/ci/docker/host-x86_64/x86_64-gnu-integration/Dockerfile index ba65ba9bed460..3132f9e0098b3 100644 --- a/src/ci/docker/host-x86_64/x86_64-gnu-integration/Dockerfile +++ b/src/ci/docker/host-x86_64/x86_64-gnu-integration/Dockerfile @@ -51,6 +51,9 @@ RUN sh /scripts/sccache.sh ENV RUST_INSTALL_DIR /checkout/obj/install RUN mkdir -p $RUST_INSTALL_DIR/etc +# Fuchsia only supports LLVM. +ENV CODEGEN_BACKENDS llvm + ENV RUST_CONFIGURE_ARGS \ --prefix=$RUST_INSTALL_DIR \ --sysconfdir=etc \ diff --git a/src/ci/run.sh b/src/ci/run.sh index 420545172e6d5..1cdcffc1a7544 100755 --- a/src/ci/run.sh +++ b/src/ci/run.sh @@ -119,7 +119,8 @@ if [ "$DEPLOY$DEPLOY_ALT" = "1" ]; then RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --set rust.verify-llvm-ir" fi - RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --set rust.codegen-backends=${CODEGEN_BACKENDS:-llvm}" + CODEGEN_BACKENDS="${CODEGEN_BACKENDS:-llvm}" + RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --set rust.codegen-backends=$CODEGEN_BACKENDS" else # We almost always want debug assertions enabled, but sometimes this takes too # long for too little benefit, so we just turn them off. @@ -144,11 +145,12 @@ else # tests as it will fail them. if [[ "${ENABLE_GCC_CODEGEN}" == "1" ]]; then # Test the Cranelift and GCC backends in CI. Bootstrap knows which targets to run tests on. - RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --set rust.codegen-backends=llvm,cranelift,gcc" + CODEGEN_BACKENDS="${CODEGEN_BACKENDS:-llvm,cranelift,gcc}" else # Test the Cranelift backend in CI. Bootstrap knows which targets to run tests on. - RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --set rust.codegen-backends=llvm,cranelift" + CODEGEN_BACKENDS="${CODEGEN_BACKENDS:-llvm,cranelift}" fi + RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --set rust.codegen-backends=$CODEGEN_BACKENDS" # We enable this for non-dist builders, since those aren't trying to produce # fresh binaries. We currently don't entirely support distributing a fresh From afd5edc8c995662760ed9cccce687637af86967f Mon Sep 17 00:00:00 2001 From: Tyler Mandry Date: Thu, 25 Jan 2024 16:40:15 -0800 Subject: [PATCH 3/8] Bump Fuchsia (includes building tests) This includes a change to the upstream build_fuchsia_from_rust_ci script that builds a minimal set of tests, to improve coverage on this builder. --- src/ci/docker/host-x86_64/x86_64-gnu-integration/Dockerfile | 1 + .../docker/host-x86_64/x86_64-gnu-integration/build-fuchsia.sh | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ci/docker/host-x86_64/x86_64-gnu-integration/Dockerfile b/src/ci/docker/host-x86_64/x86_64-gnu-integration/Dockerfile index 3132f9e0098b3..bec1c89733775 100644 --- a/src/ci/docker/host-x86_64/x86_64-gnu-integration/Dockerfile +++ b/src/ci/docker/host-x86_64/x86_64-gnu-integration/Dockerfile @@ -44,6 +44,7 @@ ENV CARGO_TARGET_X86_64_FUCHSIA_RUSTFLAGS \ ENV TARGETS=x86_64-fuchsia ENV TARGETS=$TARGETS,x86_64-unknown-linux-gnu +ENV TARGETS=$TARGETS,wasm32-unknown-unknown COPY scripts/sccache.sh /scripts/ RUN sh /scripts/sccache.sh diff --git a/src/ci/docker/host-x86_64/x86_64-gnu-integration/build-fuchsia.sh b/src/ci/docker/host-x86_64/x86_64-gnu-integration/build-fuchsia.sh index 4a246f591d717..d6de992913bf5 100755 --- a/src/ci/docker/host-x86_64/x86_64-gnu-integration/build-fuchsia.sh +++ b/src/ci/docker/host-x86_64/x86_64-gnu-integration/build-fuchsia.sh @@ -5,7 +5,7 @@ set -euf -o pipefail -INTEGRATION_SHA=66793c4894bf6204579bbee3b79956335f31c768 +INTEGRATION_SHA=56310bca298872ffb5ea02e665956d9b6dc41171 PICK_REFS=() checkout=fuchsia From f5c78955c88ac37b7422bef6ee9ec993c0a5dad2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Sat, 27 Jan 2024 14:18:33 +0200 Subject: [PATCH 4/8] Stop using derivative in rustc_pattern_analysis --- Cargo.lock | 1 - compiler/rustc_pattern_analysis/Cargo.toml | 1 - .../rustc_pattern_analysis/src/constructor.rs | 98 ++++++++++++++++++- compiler/rustc_pattern_analysis/src/lib.rs | 20 +++- compiler/rustc_pattern_analysis/src/pat.rs | 31 +++++- compiler/rustc_pattern_analysis/src/rustc.rs | 8 +- .../rustc_pattern_analysis/src/usefulness.rs | 58 ++++++++--- 7 files changed, 191 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 28068684362e4..587489a94b20e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4342,7 +4342,6 @@ dependencies = [ name = "rustc_pattern_analysis" version = "0.0.0" dependencies = [ - "derivative", "rustc-hash", "rustc_apfloat", "rustc_arena", diff --git a/compiler/rustc_pattern_analysis/Cargo.toml b/compiler/rustc_pattern_analysis/Cargo.toml index 1d0e1cb7e6a57..b9bdcb41929dc 100644 --- a/compiler/rustc_pattern_analysis/Cargo.toml +++ b/compiler/rustc_pattern_analysis/Cargo.toml @@ -5,7 +5,6 @@ edition = "2021" [dependencies] # tidy-alphabetical-start -derivative = "2.2.0" rustc-hash = "1.1.0" rustc_apfloat = "0.2.0" rustc_arena = { path = "../rustc_arena", optional = true } diff --git a/compiler/rustc_pattern_analysis/src/constructor.rs b/compiler/rustc_pattern_analysis/src/constructor.rs index e94a0373c79ff..4996015f86345 100644 --- a/compiler/rustc_pattern_analysis/src/constructor.rs +++ b/compiler/rustc_pattern_analysis/src/constructor.rs @@ -151,6 +151,7 @@ use std::cmp::{self, max, min, Ordering}; use std::fmt; use std::iter::once; +use std::mem; use smallvec::SmallVec; @@ -648,8 +649,6 @@ impl OpaqueId { /// `specialize_constructor` returns the list of fields corresponding to a pattern, given a /// constructor. `Constructor::apply` reconstructs the pattern from a pair of `Constructor` and /// `Fields`. -#[derive(derivative::Derivative)] -#[derivative(Debug(bound = ""), Clone(bound = ""), PartialEq(bound = ""))] pub enum Constructor { /// Tuples and structs. Struct, @@ -692,6 +691,101 @@ pub enum Constructor { Missing, } +impl Clone for Constructor { + fn clone(&self) -> Self { + match self { + Constructor::Struct => Constructor::Struct, + Constructor::Variant(idx) => Constructor::Variant(idx.clone()), + Constructor::Ref => Constructor::Ref, + Constructor::Slice(slice) => Constructor::Slice(slice.clone()), + Constructor::UnionField => Constructor::UnionField, + Constructor::Bool(b) => Constructor::Bool(b.clone()), + Constructor::IntRange(range) => Constructor::IntRange(range.clone()), + Constructor::F32Range(lo, hi, end) => { + Constructor::F32Range(lo.clone(), hi.clone(), end.clone()) + } + Constructor::F64Range(lo, hi, end) => { + Constructor::F64Range(lo.clone(), hi.clone(), end.clone()) + } + Constructor::Str(value) => Constructor::Str(value.clone()), + Constructor::Opaque(inner) => Constructor::Opaque(inner.clone()), + Constructor::Or => Constructor::Or, + Constructor::Wildcard => Constructor::Wildcard, + Constructor::NonExhaustive => Constructor::NonExhaustive, + Constructor::Hidden => Constructor::Hidden, + Constructor::Missing => Constructor::Missing, + } + } +} + +impl fmt::Debug for Constructor { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Constructor::Struct => f.debug_tuple("Struct").finish(), + Constructor::Variant(idx) => f.debug_tuple("Variant").field(idx).finish(), + Constructor::Ref => f.debug_tuple("Ref").finish(), + Constructor::Slice(slice) => f.debug_tuple("Slice").field(slice).finish(), + Constructor::UnionField => f.debug_tuple("UnionField").finish(), + Constructor::Bool(b) => f.debug_tuple("Bool").field(b).finish(), + Constructor::IntRange(range) => f.debug_tuple("IntRange").field(range).finish(), + Constructor::F32Range(lo, hi, end) => { + f.debug_tuple("F32Range").field(lo).field(hi).field(end).finish() + } + Constructor::F64Range(lo, hi, end) => { + f.debug_tuple("F64Range").field(lo).field(hi).field(end).finish() + } + Constructor::Str(value) => f.debug_tuple("Str").field(value).finish(), + Constructor::Opaque(inner) => f.debug_tuple("Opaque").field(inner).finish(), + Constructor::Or => f.debug_tuple("Or").finish(), + Constructor::Wildcard => f.debug_tuple("Wildcard").finish(), + Constructor::NonExhaustive => f.debug_tuple("NonExhaustive").finish(), + Constructor::Hidden => f.debug_tuple("Hidden").finish(), + Constructor::Missing => f.debug_tuple("Missing").finish(), + } + } +} + +impl PartialEq for Constructor { + fn eq(&self, other: &Self) -> bool { + (mem::discriminant(self) == mem::discriminant(other)) + && match (self, other) { + (Constructor::Struct, Constructor::Struct) => true, + (Constructor::Variant(self_variant), Constructor::Variant(other_variant)) => { + self_variant == other_variant + } + (Constructor::Ref, Constructor::Ref) => true, + (Constructor::Slice(self_slice), Constructor::Slice(other_slice)) => { + self_slice == other_slice + } + (Constructor::UnionField, Constructor::UnionField) => true, + (Constructor::Bool(self_b), Constructor::Bool(other_b)) => self_b == other_b, + (Constructor::IntRange(self_range), Constructor::IntRange(other_range)) => { + self_range == other_range + } + ( + Constructor::F32Range(self_lo, self_hi, self_end), + Constructor::F32Range(other_lo, other_hi, other_end), + ) => self_lo == other_lo && self_hi == other_hi && self_end == other_end, + ( + Constructor::F64Range(self_lo, self_hi, self_end), + Constructor::F64Range(other_lo, other_hi, other_end), + ) => self_lo == other_lo && self_hi == other_hi && self_end == other_end, + (Constructor::Str(self_value), Constructor::Str(other_value)) => { + self_value == other_value + } + (Constructor::Opaque(self_inner), Constructor::Opaque(other_inner)) => { + self_inner == other_inner + } + (Constructor::Or, Constructor::Or) => true, + (Constructor::Wildcard, Constructor::Wildcard) => true, + (Constructor::NonExhaustive, Constructor::NonExhaustive) => true, + (Constructor::Hidden, Constructor::Hidden) => true, + (Constructor::Missing, Constructor::Missing) => true, + _ => unreachable!(), + } + } +} + impl Constructor { pub(crate) fn is_non_exhaustive(&self) -> bool { matches!(self, NonExhaustive) diff --git a/compiler/rustc_pattern_analysis/src/lib.rs b/compiler/rustc_pattern_analysis/src/lib.rs index 6374874165fc1..a53d7a0d8096a 100644 --- a/compiler/rustc_pattern_analysis/src/lib.rs +++ b/compiler/rustc_pattern_analysis/src/lib.rs @@ -136,23 +136,35 @@ pub trait TypeCx: Sized + fmt::Debug { } /// Context that provides information global to a match. -#[derive(derivative::Derivative)] -#[derivative(Clone(bound = ""), Copy(bound = ""))] pub struct MatchCtxt<'a, Cx: TypeCx> { /// The context for type information. pub tycx: &'a Cx, } +impl<'a, Cx: TypeCx> Clone for MatchCtxt<'a, Cx> { + fn clone(&self) -> Self { + Self { tycx: self.tycx } + } +} + +impl<'a, Cx: TypeCx> Copy for MatchCtxt<'a, Cx> {} + /// The arm of a match expression. #[derive(Debug)] -#[derive(derivative::Derivative)] -#[derivative(Clone(bound = ""), Copy(bound = ""))] pub struct MatchArm<'p, Cx: TypeCx> { pub pat: &'p DeconstructedPat<'p, Cx>, pub has_guard: bool, pub arm_data: Cx::ArmData, } +impl<'p, Cx: TypeCx> Clone for MatchArm<'p, Cx> { + fn clone(&self) -> Self { + Self { pat: self.pat, has_guard: self.has_guard, arm_data: self.arm_data } + } +} + +impl<'p, Cx: TypeCx> Copy for MatchArm<'p, Cx> {} + /// The entrypoint for this crate. Computes whether a match is exhaustive and which of its arms are /// useful, and runs some lints. #[cfg(feature = "rustc")] diff --git a/compiler/rustc_pattern_analysis/src/pat.rs b/compiler/rustc_pattern_analysis/src/pat.rs index 1cc3107455645..d476766d466f2 100644 --- a/compiler/rustc_pattern_analysis/src/pat.rs +++ b/compiler/rustc_pattern_analysis/src/pat.rs @@ -218,8 +218,6 @@ impl<'p, Cx: TypeCx> fmt::Debug for DeconstructedPat<'p, Cx> { /// algorithm. Do not use `Wild` to represent a wildcard pattern comping from user input. /// /// This is morally `Option<&'p DeconstructedPat>` where `None` is interpreted as a wildcard. -#[derive(derivative::Derivative)] -#[derivative(Clone(bound = ""), Copy(bound = ""))] pub(crate) enum PatOrWild<'p, Cx: TypeCx> { /// A non-user-provided wildcard, created during specialization. Wild, @@ -227,6 +225,17 @@ pub(crate) enum PatOrWild<'p, Cx: TypeCx> { Pat(&'p DeconstructedPat<'p, Cx>), } +impl<'p, Cx: TypeCx> Clone for PatOrWild<'p, Cx> { + fn clone(&self) -> Self { + match self { + PatOrWild::Wild => PatOrWild::Wild, + PatOrWild::Pat(pat) => PatOrWild::Pat(pat), + } + } +} + +impl<'p, Cx: TypeCx> Copy for PatOrWild<'p, Cx> {} + impl<'p, Cx: TypeCx> PatOrWild<'p, Cx> { pub(crate) fn as_pat(&self) -> Option<&'p DeconstructedPat<'p, Cx>> { match self { @@ -289,14 +298,28 @@ impl<'p, Cx: TypeCx> fmt::Debug for PatOrWild<'p, Cx> { /// Same idea as `DeconstructedPat`, except this is a fictitious pattern built up for diagnostics /// purposes. As such they don't use interning and can be cloned. -#[derive(derivative::Derivative)] -#[derivative(Debug(bound = ""), Clone(bound = ""))] pub struct WitnessPat { ctor: Constructor, pub(crate) fields: Vec>, ty: Cx::Ty, } +impl Clone for WitnessPat { + fn clone(&self) -> Self { + Self { ctor: self.ctor.clone(), fields: self.fields.clone(), ty: self.ty.clone() } + } +} + +impl fmt::Debug for WitnessPat { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt.debug_struct("WitnessPat") + .field("ctor", &self.ctor) + .field("fields", &self.fields) + .field("ty", &self.ty) + .finish() + } +} + impl WitnessPat { pub(crate) fn new(ctor: Constructor, fields: Vec, ty: Cx::Ty) -> Self { Self { ctor, fields, ty } diff --git a/compiler/rustc_pattern_analysis/src/rustc.rs b/compiler/rustc_pattern_analysis/src/rustc.rs index ef90d24b0bf1e..223d6cefc83fe 100644 --- a/compiler/rustc_pattern_analysis/src/rustc.rs +++ b/compiler/rustc_pattern_analysis/src/rustc.rs @@ -46,11 +46,15 @@ pub type WitnessPat<'p, 'tcx> = crate::pat::WitnessPat`. #[repr(transparent)] -#[derive(derivative::Derivative)] #[derive(Clone, Copy)] -#[derivative(Debug = "transparent")] pub struct RevealedTy<'tcx>(Ty<'tcx>); +impl<'tcx> fmt::Debug for RevealedTy<'tcx> { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(fmt) + } +} + impl<'tcx> std::ops::Deref for RevealedTy<'tcx> { type Target = Ty<'tcx>; fn deref(&self) -> &Self::Target { diff --git a/compiler/rustc_pattern_analysis/src/usefulness.rs b/compiler/rustc_pattern_analysis/src/usefulness.rs index a627bdeef81e3..b15de1c0ca9d7 100644 --- a/compiler/rustc_pattern_analysis/src/usefulness.rs +++ b/compiler/rustc_pattern_analysis/src/usefulness.rs @@ -731,16 +731,26 @@ pub fn ensure_sufficient_stack(f: impl FnOnce() -> R) -> R { } /// Context that provides information local to a place under investigation. -#[derive(derivative::Derivative)] -#[derivative(Debug(bound = ""), Clone(bound = ""), Copy(bound = ""))] pub(crate) struct PlaceCtxt<'a, Cx: TypeCx> { - #[derivative(Debug = "ignore")] pub(crate) mcx: MatchCtxt<'a, Cx>, /// Type of the place under investigation. - #[derivative(Clone(clone_with = "Clone::clone"))] // See rust-derivative#90 pub(crate) ty: &'a Cx::Ty, } +impl<'a, Cx: TypeCx> Clone for PlaceCtxt<'a, Cx> { + fn clone(&self) -> Self { + Self { mcx: self.mcx, ty: self.ty } + } +} + +impl<'a, Cx: TypeCx> Copy for PlaceCtxt<'a, Cx> {} + +impl<'a, Cx: TypeCx> fmt::Debug for PlaceCtxt<'a, Cx> { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt.debug_struct("PlaceCtxt").field("ty", self.ty).finish() + } +} + impl<'a, Cx: TypeCx> PlaceCtxt<'a, Cx> { /// A `PlaceCtxt` when code other than `is_useful` needs one. #[cfg_attr(not(feature = "rustc"), allow(dead_code))] @@ -813,8 +823,6 @@ impl fmt::Display for ValidityConstraint { // The three lifetimes are: // - 'p coming from the input // - Cx global compilation context -#[derive(derivative::Derivative)] -#[derivative(Clone(bound = ""))] struct PatStack<'p, Cx: TypeCx> { // Rows of len 1 are very common, which is why `SmallVec[_; 2]` works well. pats: SmallVec<[PatOrWild<'p, Cx>; 2]>, @@ -824,6 +832,12 @@ struct PatStack<'p, Cx: TypeCx> { relevant: bool, } +impl<'p, Cx: TypeCx> Clone for PatStack<'p, Cx> { + fn clone(&self) -> Self { + Self { pats: self.pats.clone(), relevant: self.relevant } + } +} + impl<'p, Cx: TypeCx> PatStack<'p, Cx> { fn from_pattern(pat: &'p DeconstructedPat<'p, Cx>) -> Self { PatStack { pats: smallvec![PatOrWild::Pat(pat)], relevant: true } @@ -1184,10 +1198,20 @@ impl<'p, Cx: TypeCx> fmt::Debug for Matrix<'p, Cx> { /// The final `Pair(Some(_), true)` is then the resulting witness. /// /// See the top of the file for more detailed explanations and examples. -#[derive(derivative::Derivative)] -#[derivative(Debug(bound = ""), Clone(bound = ""))] struct WitnessStack(Vec>); +impl Clone for WitnessStack { + fn clone(&self) -> Self { + Self(self.0.clone()) + } +} + +impl fmt::Debug for WitnessStack { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt.debug_tuple("WitnessStack").field(&self.0).finish() + } +} + impl WitnessStack { /// Asserts that the witness contains a single pattern, and returns it. fn single_pattern(self) -> WitnessPat { @@ -1232,18 +1256,28 @@ impl WitnessStack { /// /// Just as the `Matrix` starts with a single column, by the end of the algorithm, this has a single /// column, which contains the patterns that are missing for the match to be exhaustive. -#[derive(derivative::Derivative)] -#[derivative(Debug(bound = ""), Clone(bound = ""))] struct WitnessMatrix(Vec>); +impl Clone for WitnessMatrix { + fn clone(&self) -> Self { + Self(self.0.clone()) + } +} + +impl fmt::Debug for WitnessMatrix { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt.debug_tuple("WitnessMatrix").field(&self.0).finish() + } +} + impl WitnessMatrix { /// New matrix with no witnesses. fn empty() -> Self { - WitnessMatrix(vec![]) + WitnessMatrix(Vec::new()) } /// New matrix with one `()` witness, i.e. with no columns. fn unit_witness() -> Self { - WitnessMatrix(vec![WitnessStack(vec![])]) + WitnessMatrix(vec![WitnessStack(Vec::new())]) } /// Whether this has any witnesses. From cda898b0d9a34e4a3615e879fcd4fb2da3fcead5 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sat, 27 Jan 2024 14:55:15 +0100 Subject: [PATCH 5/8] Remove unnecessary unit returns in query declarations For consistency with normal functions. --- compiler/rustc_middle/src/query/mod.rs | 28 +++++++++++++------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 1122f571fff8a..b383a6f5e52c8 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -109,7 +109,7 @@ pub use plumbing::{IntoQueryParam, TyCtxtAt, TyCtxtEnsure, TyCtxtEnsureWithValue // as they will raise an fatal error on query cycles instead. rustc_queries! { /// This exists purely for testing the interactions between span_delayed_bug and incremental. - query trigger_span_delayed_bug(key: DefId) -> () { + query trigger_span_delayed_bug(key: DefId) { desc { "triggering a span delayed bug for testing incremental" } } @@ -119,7 +119,7 @@ rustc_queries! { desc { "compute registered tools for crate" } } - query early_lint_checks(_: ()) -> () { + query early_lint_checks(_: ()) { desc { "perform lints prior to macro expansion" } } @@ -299,7 +299,7 @@ rustc_queries! { /// name. This is useful for cases were not all linting code from rustc /// was called. With the default `None` all registered lints will also /// be checked for expectation fulfillment. - query check_expectations(key: Option) -> () { + query check_expectations(key: Option) { eval_always desc { "checking lint expectations (RFC 2383)" } } @@ -906,39 +906,39 @@ rustc_queries! { } /// Performs lint checking for the module. - query lint_mod(key: LocalModDefId) -> () { + query lint_mod(key: LocalModDefId) { desc { |tcx| "linting {}", describe_as_module(key, tcx) } } - query check_unused_traits(_: ()) -> () { + query check_unused_traits(_: ()) { desc { "checking unused trait imports in crate" } } /// Checks the attributes in the module. - query check_mod_attrs(key: LocalModDefId) -> () { + query check_mod_attrs(key: LocalModDefId) { desc { |tcx| "checking attributes in {}", describe_as_module(key, tcx) } } /// Checks for uses of unstable APIs in the module. - query check_mod_unstable_api_usage(key: LocalModDefId) -> () { + query check_mod_unstable_api_usage(key: LocalModDefId) { desc { |tcx| "checking for unstable API usage in {}", describe_as_module(key, tcx) } } /// Checks the const bodies in the module for illegal operations (e.g. `if` or `loop`). - query check_mod_const_bodies(key: LocalModDefId) -> () { + query check_mod_const_bodies(key: LocalModDefId) { desc { |tcx| "checking consts in {}", describe_as_module(key, tcx) } } /// Checks the loops in the module. - query check_mod_loops(key: LocalModDefId) -> () { + query check_mod_loops(key: LocalModDefId) { desc { |tcx| "checking loops in {}", describe_as_module(key, tcx) } } - query check_mod_naked_functions(key: LocalModDefId) -> () { + query check_mod_naked_functions(key: LocalModDefId) { desc { |tcx| "checking naked functions in {}", describe_as_module(key, tcx) } } - query check_mod_privacy(key: LocalModDefId) -> () { + query check_mod_privacy(key: LocalModDefId) { desc { |tcx| "checking privacy in {}", describe_as_module(key.to_local_def_id(), tcx) } } @@ -958,7 +958,7 @@ rustc_queries! { desc { "finding live symbols in crate" } } - query check_mod_deathness(key: LocalModDefId) -> () { + query check_mod_deathness(key: LocalModDefId) { desc { |tcx| "checking deathness of variables in {}", describe_as_module(key, tcx) } } @@ -972,7 +972,7 @@ rustc_queries! { ensure_forwards_result_if_red } - query collect_mod_item_types(key: LocalModDefId) -> () { + query collect_mod_item_types(key: LocalModDefId) { desc { |tcx| "collecting item types in {}", describe_as_module(key, tcx) } } @@ -1121,7 +1121,7 @@ rustc_queries! { eval_always desc { "checking effective visibilities" } } - query check_private_in_public(_: ()) -> () { + query check_private_in_public(_: ()) { eval_always desc { "checking for private elements in public interfaces" } } From 5d8c1780fae725ce02a1c4619809347e55af67eb Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 27 Jan 2024 00:50:31 +0000 Subject: [PATCH 6/8] Make the coroutine def id of an async closure the child of the closure def id --- compiler/rustc_resolve/src/def_collector.rs | 18 ++++++++++++------ .../ui/async-await/async-closures/def-path.rs | 14 ++++++++++++++ .../async-await/async-closures/def-path.stderr | 17 +++++++++++++++++ 3 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 tests/ui/async-await/async-closures/def-path.rs create mode 100644 tests/ui/async-await/async-closures/def-path.stderr diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index b77102c085c59..42ace9bb22fa9 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -289,12 +289,18 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> { // we must create two defs. let closure_def = self.create_def(expr.id, kw::Empty, DefKind::Closure, expr.span); match closure.coroutine_kind { - Some(coroutine_kind) => self.create_def( - coroutine_kind.closure_id(), - kw::Empty, - DefKind::Closure, - expr.span, - ), + Some(coroutine_kind) => { + self.with_parent(closure_def, |this| { + let coroutine_def = this.create_def( + coroutine_kind.closure_id(), + kw::Empty, + DefKind::Closure, + expr.span, + ); + this.with_parent(coroutine_def, |this| visit::walk_expr(this, expr)); + }); + return; + } None => closure_def, } } diff --git a/tests/ui/async-await/async-closures/def-path.rs b/tests/ui/async-await/async-closures/def-path.rs new file mode 100644 index 0000000000000..2883a1715b0ac --- /dev/null +++ b/tests/ui/async-await/async-closures/def-path.rs @@ -0,0 +1,14 @@ +// compile-flags: -Zverbose-internals +// edition:2021 + +#![feature(async_closure)] + +fn main() { + let x = async || {}; + //~^ NOTE the expected `async` closure body + let () = x(); + //~^ ERROR mismatched types + //~| NOTE this expression has type `{static main::{closure#0}::{closure#0} upvar_tys= + //~| NOTE expected `async` closure body, found `()` + //~| NOTE expected `async` closure body `{static main::{closure#0}::{closure#0} +} diff --git a/tests/ui/async-await/async-closures/def-path.stderr b/tests/ui/async-await/async-closures/def-path.stderr new file mode 100644 index 0000000000000..4b37e50aac459 --- /dev/null +++ b/tests/ui/async-await/async-closures/def-path.stderr @@ -0,0 +1,17 @@ +error[E0308]: mismatched types + --> $DIR/def-path.rs:9:9 + | +LL | let x = async || {}; + | -- the expected `async` closure body +LL | +LL | let () = x(); + | ^^ --- this expression has type `{static main::{closure#0}::{closure#0} upvar_tys=?7t witness=?8t}` + | | + | expected `async` closure body, found `()` + | + = note: expected `async` closure body `{static main::{closure#0}::{closure#0} upvar_tys=?7t witness=?8t}` + found unit type `()` + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0308`. From 5f8030dcc9eaa68975279134d76c4a1eb941081b Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 27 Jan 2024 17:46:50 +0300 Subject: [PATCH 7/8] hir: Remove unnecessary `HirId` from `hir::Let` It has 1-to-1 correspondence to its expression id. Also remove mostly useless `visit_let_expr`. --- compiler/rustc_ast_lowering/src/expr.rs | 1 - compiler/rustc_hir/src/hir.rs | 1 - compiler/rustc_hir/src/intravisit.rs | 18 ++++++------------ compiler/rustc_hir_typeck/src/expr.rs | 6 +++--- compiler/rustc_hir_typeck/src/gather_locals.rs | 14 ++++++++------ compiler/rustc_passes/src/hir_stats.rs | 5 ----- 6 files changed, 17 insertions(+), 28 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index cc172b376573f..0ad4a59c17eb1 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -153,7 +153,6 @@ impl<'hir> LoweringContext<'_, 'hir> { } ExprKind::Let(pat, scrutinee, span, is_recovered) => { hir::ExprKind::Let(self.arena.alloc(hir::Let { - hir_id: self.next_id(), span: self.lower_span(*span), pat: self.lower_pat(pat), ty: None, diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 23881cfd7dfd3..e34ea73952f5a 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -1273,7 +1273,6 @@ pub struct Arm<'hir> { /// desugaring to if-let. Only let-else supports the type annotation at present. #[derive(Debug, Clone, Copy, HashStable_Generic)] pub struct Let<'hir> { - pub hir_id: HirId, pub span: Span, pub pat: &'hir Pat<'hir>, pub ty: Option<&'hir Ty<'hir>>, diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index 116de6fb04d99..38ec325630b38 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -341,9 +341,6 @@ pub trait Visitor<'v>: Sized { fn visit_expr(&mut self, ex: &'v Expr<'v>) { walk_expr(self, ex) } - fn visit_let_expr(&mut self, lex: &'v Let<'v>) { - walk_let_expr(self, lex) - } fn visit_expr_field(&mut self, field: &'v ExprField<'v>) { walk_expr_field(self, field) } @@ -729,7 +726,12 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) ExprKind::DropTemps(ref subexpression) => { visitor.visit_expr(subexpression); } - ExprKind::Let(ref let_expr) => visitor.visit_let_expr(let_expr), + ExprKind::Let(Let { span: _, pat, ty, init, is_recovered: _ }) => { + // match the visit order in walk_local + visitor.visit_expr(init); + visitor.visit_pat(pat); + walk_list!(visitor, visit_ty, ty); + } ExprKind::If(ref cond, ref then, ref else_opt) => { visitor.visit_expr(cond); visitor.visit_expr(then); @@ -806,14 +808,6 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) } } -pub fn walk_let_expr<'v, V: Visitor<'v>>(visitor: &mut V, let_expr: &'v Let<'v>) { - // match the visit order in walk_local - visitor.visit_expr(let_expr.init); - visitor.visit_id(let_expr.hir_id); - visitor.visit_pat(let_expr.pat); - walk_list!(visitor, visit_ty, let_expr.ty); -} - pub fn walk_expr_field<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v ExprField<'v>) { visitor.visit_id(field.hir_id); visitor.visit_ident(field.ident); diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index 3bc259c17b774..3bc4032558885 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -320,7 +320,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } ExprKind::Ret(ref expr_opt) => self.check_expr_return(expr_opt.as_deref(), expr), ExprKind::Become(call) => self.check_expr_become(call, expr), - ExprKind::Let(let_expr) => self.check_expr_let(let_expr), + ExprKind::Let(let_expr) => self.check_expr_let(let_expr, expr.hir_id), ExprKind::Loop(body, _, source, _) => { self.check_expr_loop(body, source, expected, expr) } @@ -1259,12 +1259,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } - pub(super) fn check_expr_let(&self, let_expr: &'tcx hir::Let<'tcx>) -> Ty<'tcx> { + pub(super) fn check_expr_let(&self, let_expr: &'tcx hir::Let<'tcx>, hir_id: HirId) -> Ty<'tcx> { // for let statements, this is done in check_stmt let init = let_expr.init; self.warn_if_unreachable(init.hir_id, init.span, "block in `let` expression"); // otherwise check exactly as a let statement - self.check_decl(let_expr.into()); + self.check_decl((let_expr, hir_id).into()); // but return a bool, for this is a boolean expression if let Some(error_guaranteed) = let_expr.is_recovered { self.set_tainted_by_errors(error_guaranteed); diff --git a/compiler/rustc_hir_typeck/src/gather_locals.rs b/compiler/rustc_hir_typeck/src/gather_locals.rs index 52dc1e8591656..f9af357f0e79e 100644 --- a/compiler/rustc_hir_typeck/src/gather_locals.rs +++ b/compiler/rustc_hir_typeck/src/gather_locals.rs @@ -48,9 +48,9 @@ impl<'a> From<&'a hir::Local<'a>> for Declaration<'a> { } } -impl<'a> From<&'a hir::Let<'a>> for Declaration<'a> { - fn from(let_expr: &'a hir::Let<'a>) -> Self { - let hir::Let { hir_id, pat, ty, span, init, is_recovered: _ } = *let_expr; +impl<'a> From<(&'a hir::Let<'a>, hir::HirId)> for Declaration<'a> { + fn from((let_expr, hir_id): (&'a hir::Let<'a>, hir::HirId)) -> Self { + let hir::Let { pat, ty, span, init, is_recovered: _ } = *let_expr; Declaration { hir_id, pat, ty, span, init: Some(init), origin: DeclOrigin::LetExpr } } } @@ -125,9 +125,11 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> { intravisit::walk_local(self, local) } - fn visit_let_expr(&mut self, let_expr: &'tcx hir::Let<'tcx>) { - self.declare(let_expr.into()); - intravisit::walk_let_expr(self, let_expr); + fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) { + if let hir::ExprKind::Let(let_expr) = expr.kind { + self.declare((let_expr, expr.hir_id).into()); + } + intravisit::walk_expr(self, expr) } fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) { diff --git a/compiler/rustc_passes/src/hir_stats.rs b/compiler/rustc_passes/src/hir_stats.rs index 528a52f42255e..e94d8c4c932f9 100644 --- a/compiler/rustc_passes/src/hir_stats.rs +++ b/compiler/rustc_passes/src/hir_stats.rs @@ -328,11 +328,6 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> { hir_visit::walk_expr(self, e) } - fn visit_let_expr(&mut self, lex: &'v hir::Let<'v>) { - self.record("Let", Id::Node(lex.hir_id), lex); - hir_visit::walk_let_expr(self, lex) - } - fn visit_expr_field(&mut self, f: &'v hir::ExprField<'v>) { self.record("ExprField", Id::Node(f.hir_id), f); hir_visit::walk_expr_field(self, f) From b2b5b91bfbe5b39d17be3a14108fe7823627e0d8 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 27 Jan 2024 18:59:20 +0300 Subject: [PATCH 8/8] hir: Use `InferArg` in `ArrayLen::Infer` --- compiler/rustc_ast_lowering/src/lib.rs | 5 ++++- compiler/rustc_hir/src/hir.rs | 8 +++++--- compiler/rustc_hir/src/intravisit.rs | 2 +- compiler/rustc_hir_analysis/src/astconv/mod.rs | 2 +- compiler/rustc_hir_analysis/src/collect.rs | 4 ++-- compiler/rustc_hir_pretty/src/lib.rs | 2 +- compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs | 2 +- src/librustdoc/clean/mod.rs | 2 +- 8 files changed, 16 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index ee1f5d5bd7a3f..3621844efc8d2 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -2305,7 +2305,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { match c.value.kind { ExprKind::Underscore => { if self.tcx.features().generic_arg_infer { - hir::ArrayLen::Infer(self.lower_node_id(c.id), self.lower_span(c.value.span)) + hir::ArrayLen::Infer(hir::InferArg { + hir_id: self.lower_node_id(c.id), + span: self.lower_span(c.value.span), + }) } else { feature_err( &self.tcx.sess, diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index e34ea73952f5a..681e228a0f2ff 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -1531,14 +1531,16 @@ pub type Lit = Spanned; #[derive(Copy, Clone, Debug, HashStable_Generic)] pub enum ArrayLen { - Infer(HirId, Span), + Infer(InferArg), Body(AnonConst), } impl ArrayLen { pub fn hir_id(&self) -> HirId { match self { - &ArrayLen::Infer(hir_id, _) | &ArrayLen::Body(AnonConst { hir_id, .. }) => hir_id, + ArrayLen::Infer(InferArg { hir_id, .. }) | ArrayLen::Body(AnonConst { hir_id, .. }) => { + *hir_id + } } } } @@ -2423,7 +2425,7 @@ impl<'hir> Ty<'hir> { TyKind::Infer => true, TyKind::Slice(ty) => ty.is_suggestable_infer_ty(), TyKind::Array(ty, length) => { - ty.is_suggestable_infer_ty() || matches!(length, ArrayLen::Infer(_, _)) + ty.is_suggestable_infer_ty() || matches!(length, ArrayLen::Infer(..)) } TyKind::Tup(tys) => tys.iter().any(Self::is_suggestable_infer_ty), TyKind::Ptr(mut_ty) | TyKind::Ref(_, mut_ty) => mut_ty.ty.is_suggestable_infer_ty(), diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index 38ec325630b38..27c834d848fc4 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -669,7 +669,7 @@ pub fn walk_pat_field<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v PatField<' pub fn walk_array_len<'v, V: Visitor<'v>>(visitor: &mut V, len: &'v ArrayLen) { match len { - &ArrayLen::Infer(hir_id, _span) => visitor.visit_id(hir_id), + ArrayLen::Infer(InferArg { hir_id, span: _ }) => visitor.visit_id(*hir_id), ArrayLen::Body(c) => visitor.visit_anon_const(c), } } diff --git a/compiler/rustc_hir_analysis/src/astconv/mod.rs b/compiler/rustc_hir_analysis/src/astconv/mod.rs index 7c97c2a1a6ff2..3ba63c659c776 100644 --- a/compiler/rustc_hir_analysis/src/astconv/mod.rs +++ b/compiler/rustc_hir_analysis/src/astconv/mod.rs @@ -2529,7 +2529,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { } hir::TyKind::Array(ty, length) => { let length = match length { - &hir::ArrayLen::Infer(_, span) => self.ct_infer(tcx.types.usize, None, span), + hir::ArrayLen::Infer(inf) => self.ct_infer(tcx.types.usize, None, inf.span), hir::ArrayLen::Body(constant) => { ty::Const::from_anon_const(tcx, constant.def_id) } diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index d8ce2307995c5..8d862d5eb715f 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -146,8 +146,8 @@ impl<'v> Visitor<'v> for HirPlaceholderCollector { } } fn visit_array_length(&mut self, length: &'v hir::ArrayLen) { - if let &hir::ArrayLen::Infer(_, span) = length { - self.0.push(span); + if let hir::ArrayLen::Infer(inf) = length { + self.0.push(inf.span); } intravisit::walk_array_len(self, length) } diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index e76303bc6dfa1..9d0c5cb0f32b0 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -956,7 +956,7 @@ impl<'a> State<'a> { fn print_array_length(&mut self, len: &hir::ArrayLen) { match len { - hir::ArrayLen::Infer(_, _) => self.word("_"), + hir::ArrayLen::Infer(..) => self.word("_"), hir::ArrayLen::Body(ct) => self.print_anon_const(ct), } } diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs index c6b9197d0e986..e99489ee3c03c 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs @@ -405,7 +405,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { pub fn array_length_to_const(&self, length: &hir::ArrayLen) -> ty::Const<'tcx> { match length { - &hir::ArrayLen::Infer(_, span) => self.ct_infer(self.tcx.types.usize, None, span), + hir::ArrayLen::Infer(inf) => self.ct_infer(self.tcx.types.usize, None, inf.span), hir::ArrayLen::Body(anon_const) => { let span = self.tcx.def_span(anon_const.def_id); let c = ty::Const::from_anon_const(self.tcx, anon_const.def_id); diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 8706abda97963..bc3c455f3980b 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1835,7 +1835,7 @@ pub(crate) fn clean_ty<'tcx>(ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> T TyKind::Slice(ty) => Slice(Box::new(clean_ty(ty, cx))), TyKind::Array(ty, ref length) => { let length = match length { - hir::ArrayLen::Infer(_, _) => "_".to_string(), + hir::ArrayLen::Infer(..) => "_".to_string(), hir::ArrayLen::Body(anon_const) => { // NOTE(min_const_generics): We can't use `const_eval_poly` for constants // as we currently do not supply the parent generics to anonymous constants