Skip to content

Commit

Permalink
implement lowering of guard patterns to THIR
Browse files Browse the repository at this point in the history
  • Loading branch information
max-niederman committed Oct 3, 2024
1 parent ef4394d commit 691ee1c
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 109 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use rustc_middle::util::Providers;
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }

pub fn provide(providers: &mut Providers) {
providers.check_match = thir::pattern::check_match;
providers.check_match = thir::check_match::check_match;
providers.lit_to_const = thir::constant::lit_to_const;
providers.hooks.build_mir = build::mir_build;
providers.closure_saved_names_of_captured_variables =
Expand Down
14 changes: 4 additions & 10 deletions compiler/rustc_mir_build/src/thir/cx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
//! structures into the THIR. The `builder` is generally ignorant of the tcx,
//! etc., and instead goes through the `Cx` for most of its work.

mod block;
mod expr;
mod pattern;

use rustc_data_structures::steal::Steal;
use rustc_errors::ErrorGuaranteed;
use rustc_hir as hir;
Expand All @@ -13,9 +17,7 @@ use rustc_middle::bug;
use rustc_middle::middle::region;
use rustc_middle::thir::*;
use rustc_middle::ty::{self, RvalueScopes, TyCtxt};
use tracing::instrument;

use crate::thir::pattern::pat_from_hir;
use crate::thir::util::UserAnnotatedTyHelpers;

pub(crate) fn thir_body(
Expand Down Expand Up @@ -109,11 +111,6 @@ impl<'tcx> Cx<'tcx> {
}
}

#[instrument(level = "debug", skip(self))]
fn pattern_from_hir(&mut self, p: &'tcx hir::Pat<'tcx>) -> Box<Pat<'tcx>> {
pat_from_hir(self.tcx, self.param_env, self.typeck_results(), p)
}

fn closure_env_param(&self, owner_def: LocalDefId, expr_id: HirId) -> Option<Param<'tcx>> {
if self.tcx.def_kind(owner_def) != DefKind::Closure {
return None;
Expand Down Expand Up @@ -206,6 +203,3 @@ impl<'tcx> UserAnnotatedTyHelpers<'tcx> for Cx<'tcx> {
self.typeck_results
}
}

mod block;
mod expr;
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ use rustc_trait_selection::traits::ObligationCause;
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
use tracing::{debug, instrument, trace};

use super::PatCtxt;
use super::PatCx;
use crate::errors::{
ConstPatternDependsOnGenericParameter, CouldNotEvalConstPattern, InvalidPattern, NaNPattern,
PointerPattern, TypeNotPartialEq, TypeNotStructural, UnionPattern, UnsizedPattern,
};

impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
impl<'a, 'tcx> PatCx<'a, 'tcx> {
/// Converts a constant to a pattern (if possible).
/// This means aggregate values (like structs and enums) are converted
/// to a pattern that matches the value (as if you'd compared via structural equality).
Expand All @@ -36,7 +36,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
id: hir::HirId,
span: Span,
) -> Box<Pat<'tcx>> {
let infcx = self.tcx.infer_ctxt().build();
let infcx = self.cx.tcx.infer_ctxt().build();
let mut convert = ConstToPat::new(self, id, span, infcx);
convert.to_pat(c, ty)
}
Expand All @@ -54,19 +54,15 @@ struct ConstToPat<'tcx> {
}

impl<'tcx> ConstToPat<'tcx> {
fn new(
pat_ctxt: &PatCtxt<'_, 'tcx>,
id: hir::HirId,
span: Span,
infcx: InferCtxt<'tcx>,
) -> Self {
trace!(?pat_ctxt.typeck_results.hir_owner);
fn new(pat_ctxt: &PatCx<'_, 'tcx>, id: hir::HirId, span: Span, infcx: InferCtxt<'tcx>) -> Self {
trace!(?pat_ctxt.cx.typeck_results.hir_owner);
ConstToPat {
id,
span,
infcx,
param_env: pat_ctxt.param_env,
param_env: pat_ctxt.cx.param_env,
treat_byte_string_as_slice: pat_ctxt
.cx
.typeck_results
.treat_byte_string_as_slice
.contains(&id.local_id),
Expand Down
Loading

0 comments on commit 691ee1c

Please sign in to comment.