From 95c8c65139c10e4de44367fead8dff88511e6d46 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 17 Jun 2021 17:37:14 +0200 Subject: Nest all the or-patterns! --- crates/hir_ty/src/consteval.rs | 3 +- .../src/diagnostics/match_check/deconstruct_pat.rs | 9 ++--- crates/hir_ty/src/infer/coerce.rs | 8 ++--- crates/hir_ty/src/infer/expr.rs | 16 ++++----- crates/hir_ty/src/infer/pat.rs | 9 ++--- crates/hir_ty/src/op.rs | 38 +++++++++------------- 6 files changed, 33 insertions(+), 50 deletions(-) (limited to 'crates/hir_ty') diff --git a/crates/hir_ty/src/consteval.rs b/crates/hir_ty/src/consteval.rs index 6f0bf8f8c..ab1afce08 100644 --- a/crates/hir_ty/src/consteval.rs +++ b/crates/hir_ty/src/consteval.rs @@ -38,8 +38,7 @@ impl ConstExt for Const { // FIXME: support more than just evaluating literals pub fn eval_usize(expr: &Expr) -> Option { match expr { - Expr::Literal(Literal::Uint(v, None)) - | Expr::Literal(Literal::Uint(v, Some(BuiltinUint::Usize))) => (*v).try_into().ok(), + Expr::Literal(Literal::Uint(v, None | Some(BuiltinUint::Usize))) => (*v).try_into().ok(), _ => None, } } diff --git a/crates/hir_ty/src/diagnostics/match_check/deconstruct_pat.rs b/crates/hir_ty/src/diagnostics/match_check/deconstruct_pat.rs index 471cd4921..e3d640a79 100644 --- a/crates/hir_ty/src/diagnostics/match_check/deconstruct_pat.rs +++ b/crates/hir_ty/src/diagnostics/match_check/deconstruct_pat.rs @@ -84,10 +84,7 @@ impl IntRange { #[inline] fn is_integral(ty: &Ty) -> bool { match ty.kind(&Interner) { - TyKind::Scalar(Scalar::Char) - | TyKind::Scalar(Scalar::Int(_)) - | TyKind::Scalar(Scalar::Uint(_)) - | TyKind::Scalar(Scalar::Bool) => true, + TyKind::Scalar(Scalar::Char | Scalar::Int(_) | Scalar::Uint(_) | Scalar::Bool) => true, _ => false, } } @@ -381,7 +378,7 @@ impl Constructor { // Wildcards cover anything (_, Wildcard) => true, // The missing ctors are not covered by anything in the matrix except wildcards. - (Missing, _) | (Wildcard, _) => false, + (Missing | Wildcard, _) => false, (Single, Single) => true, (Variant(self_id), Variant(other_id)) => self_id == other_id, @@ -523,7 +520,7 @@ impl SplitWildcard { } } TyKind::Scalar(Scalar::Char) => unhandled(), - TyKind::Scalar(Scalar::Int(..)) | TyKind::Scalar(Scalar::Uint(..)) => unhandled(), + TyKind::Scalar(Scalar::Int(..) | Scalar::Uint(..)) => unhandled(), TyKind::Never if !cx.feature_exhaustive_patterns() && !pcx.is_top_level => { smallvec![NonExhaustive] } diff --git a/crates/hir_ty/src/infer/coerce.rs b/crates/hir_ty/src/infer/coerce.rs index 4b7f31521..7be914451 100644 --- a/crates/hir_ty/src/infer/coerce.rs +++ b/crates/hir_ty/src/infer/coerce.rs @@ -47,10 +47,7 @@ impl<'a> InferenceContext<'a> { // pointers to have a chance at getting a match. See // https://github.com/rust-lang/rust/blob/7b805396bf46dce972692a6846ce2ad8481c5f85/src/librustc_typeck/check/coercion.rs#L877-L916 let sig = match (ty1.kind(&Interner), ty2.kind(&Interner)) { - (TyKind::FnDef(..), TyKind::FnDef(..)) - | (TyKind::Closure(..), TyKind::FnDef(..)) - | (TyKind::FnDef(..), TyKind::Closure(..)) - | (TyKind::Closure(..), TyKind::Closure(..)) => { + (TyKind::FnDef(..) | TyKind::Closure(..), TyKind::FnDef(..) | TyKind::Closure(..)) => { // FIXME: we're ignoring safety here. To be more correct, if we have one FnDef and one Closure, // we should be coercing the closure to a fn pointer of the safety of the FnDef cov_mark::hit!(coerce_fn_reification); @@ -448,8 +445,7 @@ fn safe_to_unsafe_fn_ty(fn_ty: FnPointer) -> FnPointer { fn coerce_mutabilities(from: Mutability, to: Mutability) -> Result<(), TypeError> { match (from, to) { - (Mutability::Mut, Mutability::Mut) - | (Mutability::Mut, Mutability::Not) + (Mutability::Mut, Mutability::Mut | Mutability::Not) | (Mutability::Not, Mutability::Not) => Ok(()), (Mutability::Not, Mutability::Mut) => Err(TypeError), } diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index 5ea2e5934..4e4f6e5a4 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -593,11 +593,11 @@ impl<'a> InferenceContext<'a> { UnaryOp::Neg => { match inner_ty.kind(&Interner) { // Fast path for builtins - TyKind::Scalar(Scalar::Int(_)) - | TyKind::Scalar(Scalar::Uint(_)) - | TyKind::Scalar(Scalar::Float(_)) - | TyKind::InferenceVar(_, TyVariableKind::Integer) - | TyKind::InferenceVar(_, TyVariableKind::Float) => inner_ty, + TyKind::Scalar(Scalar::Int(_) | Scalar::Uint(_) | Scalar::Float(_)) + | TyKind::InferenceVar( + _, + TyVariableKind::Integer | TyVariableKind::Float, + ) => inner_ty, // Otherwise we resolve via the std::ops::Neg trait _ => self .resolve_associated_type(inner_ty, self.resolve_ops_neg_output()), @@ -606,9 +606,7 @@ impl<'a> InferenceContext<'a> { UnaryOp::Not => { match inner_ty.kind(&Interner) { // Fast path for builtins - TyKind::Scalar(Scalar::Bool) - | TyKind::Scalar(Scalar::Int(_)) - | TyKind::Scalar(Scalar::Uint(_)) + TyKind::Scalar(Scalar::Bool | Scalar::Int(_) | Scalar::Uint(_)) | TyKind::InferenceVar(_, TyVariableKind::Integer) => inner_ty, // Otherwise we resolve via the std::ops::Not trait _ => self @@ -735,7 +733,7 @@ impl<'a> InferenceContext<'a> { Expr::Array(array) => { let elem_ty = match expected.to_option(&mut self.table).as_ref().map(|t| t.kind(&Interner)) { - Some(TyKind::Array(st, _)) | Some(TyKind::Slice(st)) => st.clone(), + Some(TyKind::Array(st, _) | TyKind::Slice(st)) => st.clone(), _ => self.table.new_type_var(), }; diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs index 035f4ded6..58cb23e9d 100644 --- a/crates/hir_ty/src/infer/pat.rs +++ b/crates/hir_ty/src/infer/pat.rs @@ -297,10 +297,11 @@ fn is_non_ref_pat(body: &hir_def::body::Body, pat: PatId) -> bool { Expr::Literal(Literal::String(..)) => false, _ => true, }, - Pat::Bind { mode: BindingAnnotation::Mutable, subpat: Some(subpat), .. } - | Pat::Bind { mode: BindingAnnotation::Unannotated, subpat: Some(subpat), .. } => { - is_non_ref_pat(body, *subpat) - } + Pat::Bind { + mode: BindingAnnotation::Mutable | BindingAnnotation::Unannotated, + subpat: Some(subpat), + .. + } => is_non_ref_pat(body, *subpat), Pat::Wild | Pat::Bind { .. } | Pat::Ref { .. } | Pat::Box { .. } | Pat::Missing => false, } } diff --git a/crates/hir_ty/src/op.rs b/crates/hir_ty/src/op.rs index 0222de2bc..5ef6342d5 100644 --- a/crates/hir_ty/src/op.rs +++ b/crates/hir_ty/src/op.rs @@ -8,17 +8,15 @@ pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty { match op { BinaryOp::LogicOp(_) | BinaryOp::CmpOp(_) => TyKind::Scalar(Scalar::Bool).intern(&Interner), BinaryOp::Assignment { .. } => TyBuilder::unit(), - BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => { + BinaryOp::ArithOp(ArithOp::Shl | ArithOp::Shr) => { // all integer combinations are valid here if matches!( lhs_ty.kind(&Interner), - TyKind::Scalar(Scalar::Int(_)) - | TyKind::Scalar(Scalar::Uint(_)) + TyKind::Scalar(Scalar::Int(_) | Scalar::Uint(_)) | TyKind::InferenceVar(_, TyVariableKind::Integer) ) && matches!( rhs_ty.kind(&Interner), - TyKind::Scalar(Scalar::Int(_)) - | TyKind::Scalar(Scalar::Uint(_)) + TyKind::Scalar(Scalar::Int(_) | Scalar::Uint(_)) | TyKind::InferenceVar(_, TyVariableKind::Integer) ) { lhs_ty @@ -32,15 +30,15 @@ pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty { | (TyKind::Scalar(Scalar::Uint(_)), TyKind::Scalar(Scalar::Uint(_))) | (TyKind::Scalar(Scalar::Float(_)), TyKind::Scalar(Scalar::Float(_))) => rhs_ty, // ({int}, int) | ({int}, uint) - (TyKind::InferenceVar(_, TyVariableKind::Integer), TyKind::Scalar(Scalar::Int(_))) - | (TyKind::InferenceVar(_, TyVariableKind::Integer), TyKind::Scalar(Scalar::Uint(_))) => { - rhs_ty - } + ( + TyKind::InferenceVar(_, TyVariableKind::Integer), + TyKind::Scalar(Scalar::Int(_) | Scalar::Uint(_)), + ) => rhs_ty, // (int, {int}) | (uint, {int}) - (TyKind::Scalar(Scalar::Int(_)), TyKind::InferenceVar(_, TyVariableKind::Integer)) - | (TyKind::Scalar(Scalar::Uint(_)), TyKind::InferenceVar(_, TyVariableKind::Integer)) => { - lhs_ty - } + ( + TyKind::Scalar(Scalar::Int(_) | Scalar::Uint(_)), + TyKind::InferenceVar(_, TyVariableKind::Integer), + ) => lhs_ty, // ({float} | float) (TyKind::InferenceVar(_, TyVariableKind::Float), TyKind::Scalar(Scalar::Float(_))) => { rhs_ty @@ -69,21 +67,15 @@ pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty { BinaryOp::Assignment { op: None } => lhs_ty, BinaryOp::CmpOp(CmpOp::Eq { .. }) => match lhs_ty.kind(&Interner) { TyKind::Scalar(_) | TyKind::Str => lhs_ty, - TyKind::InferenceVar(_, TyVariableKind::Integer) - | TyKind::InferenceVar(_, TyVariableKind::Float) => lhs_ty, + TyKind::InferenceVar(_, TyVariableKind::Integer | TyVariableKind::Float) => lhs_ty, _ => TyKind::Error.intern(&Interner), }, - BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => { - TyKind::Error.intern(&Interner) - } + BinaryOp::ArithOp(ArithOp::Shl | ArithOp::Shr) => TyKind::Error.intern(&Interner), BinaryOp::CmpOp(CmpOp::Ord { .. }) | BinaryOp::Assignment { op: Some(_) } | BinaryOp::ArithOp(_) => match lhs_ty.kind(&Interner) { - TyKind::Scalar(Scalar::Int(_)) - | TyKind::Scalar(Scalar::Uint(_)) - | TyKind::Scalar(Scalar::Float(_)) => lhs_ty, - TyKind::InferenceVar(_, TyVariableKind::Integer) - | TyKind::InferenceVar(_, TyVariableKind::Float) => lhs_ty, + TyKind::Scalar(Scalar::Int(_) | Scalar::Uint(_) | Scalar::Float(_)) => lhs_ty, + TyKind::InferenceVar(_, TyVariableKind::Integer | TyVariableKind::Float) => lhs_ty, _ => TyKind::Error.intern(&Interner), }, } -- cgit v1.2.3