Skip to content

Commit

Permalink
Auto merge of #54533 - ljedrz:cleanup_librustc_typeck_check, r=davidtwco
Browse files Browse the repository at this point in the history
A few cleanups and minor improvements to typeck/check

- turn a `loop` into a `while let`
- turn a `push_back` loop into an `extend`
- turn a few `push` loops into collected iterators
- prefer `vec![x; n]` to `(0..n).map(|_| x).collect()`
- combine two loops doing the same thing on 2 data sets using `chain`
- use `unwrap_or` where applicable and readable
- add a `potentially_plural_count` helper function to simplify several `format!()` calls
- prefer `to_owned` to `to_string` for string literals
- change `match` to `if let` where only one branch matters
- a few other minor improvements
- whitespace fixes
  • Loading branch information
bors committed Sep 27, 2018
2 parents c9865b1 + 3527276 commit f1694ea
Show file tree
Hide file tree
Showing 19 changed files with 450 additions and 581 deletions.
62 changes: 30 additions & 32 deletions src/librustc_typeck/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,35 +81,33 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
//
// See the examples in `run-pass/match-defbm*.rs`.
let mut pat_adjustments = vec![];
expected = loop {
while let ty::Ref(_, inner_ty, inner_mutability) = exp_ty.sty {
debug!("inspecting {:?} with type {:?}", exp_ty, exp_ty.sty);
match exp_ty.sty {
ty::Ref(_, inner_ty, inner_mutability) => {
debug!("current discriminant is Ref, inserting implicit deref");
// Preserve the reference type. We'll need it later during HAIR lowering.
pat_adjustments.push(exp_ty);

exp_ty = inner_ty;
def_bm = match def_bm {
// If default binding mode is by value, make it `ref` or `ref mut`
// (depending on whether we observe `&` or `&mut`).
ty::BindByValue(_) =>
ty::BindByReference(inner_mutability),

// Once a `ref`, always a `ref`. This is because a `& &mut` can't mutate
// the underlying value.
ty::BindByReference(hir::Mutability::MutImmutable) =>
ty::BindByReference(hir::Mutability::MutImmutable),

// When `ref mut`, stay a `ref mut` (on `&mut`) or downgrade to `ref`
// (on `&`).
ty::BindByReference(hir::Mutability::MutMutable) =>
ty::BindByReference(inner_mutability),
};
},
_ => break exp_ty,
}
};

debug!("current discriminant is Ref, inserting implicit deref");
// Preserve the reference type. We'll need it later during HAIR lowering.
pat_adjustments.push(exp_ty);

exp_ty = inner_ty;
def_bm = match def_bm {
// If default binding mode is by value, make it `ref` or `ref mut`
// (depending on whether we observe `&` or `&mut`).
ty::BindByValue(_) =>
ty::BindByReference(inner_mutability),

// Once a `ref`, always a `ref`. This is because a `& &mut` can't mutate
// the underlying value.
ty::BindByReference(hir::Mutability::MutImmutable) =>
ty::BindByReference(hir::Mutability::MutImmutable),

// When `ref mut`, stay a `ref mut` (on `&mut`) or downgrade to `ref`
// (on `&`).
ty::BindByReference(hir::Mutability::MutMutable) =>
ty::BindByReference(inner_mutability),
};
}
expected = exp_ty;

if pat_adjustments.len() > 0 {
debug!("default binding mode is now {:?}", def_bm);
self.inh.tables.borrow_mut()
Expand Down Expand Up @@ -153,7 +151,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
if let ty::Ref(_, r_ty, _) = expected_ty.sty {
if let ty::Slice(_) = r_ty.sty {
pat_ty = tcx.mk_imm_ref(tcx.types.re_static,
tcx.mk_slice(tcx.types.u8))
tcx.mk_slice(tcx.types.u8))
}
}
}
Expand Down Expand Up @@ -294,7 +292,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {

let element_tys_iter = (0..max_len).map(|_| self.next_ty_var(
// FIXME: MiscVariable for now, obtaining the span and name information
// from all tuple elements isn't trivial.
// from all tuple elements isn't trivial.
TypeVariableOrigin::TypeInference(pat.span)));
let element_tys = tcx.mk_type_list(element_tys_iter);
let pat_ty = tcx.mk_ty(ty::Tuple(element_tys));
Expand Down Expand Up @@ -394,7 +392,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
tcx.sess, pat.span, E0527,
"pattern requires {} elements but array has {}",
min_len, size)
.span_label(pat.span, format!("expected {} elements",size))
.span_label(pat.span, format!("expected {} elements", size))
.emit();
}
(inner_ty, tcx.types.err)
Expand Down Expand Up @@ -857,7 +855,7 @@ https://doc.rust-lang.org/reference/types.html#trait-objects");
subpats.len(), subpats_ending, def.kind_name(),
variant.fields.len(), fields_ending)
.span_label(pat.span, format!("expected {} field{}, found {}",
variant.fields.len(), fields_ending, subpats.len()))
variant.fields.len(), fields_ending, subpats.len()))
.emit();
on_error();
return tcx.types.err;
Expand Down
51 changes: 24 additions & 27 deletions src/librustc_typeck/check/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,34 +166,31 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
None => continue,
};

match self.lookup_method_in_trait(call_expr.span,
method_name,
trait_def_id,
adjusted_ty,
None) {
None => continue,
Some(ok) => {
let method = self.register_infer_ok_obligations(ok);
let mut autoref = None;
if borrow {
if let ty::Ref(region, _, mutbl) = method.sig.inputs()[0].sty {
let mutbl = match mutbl {
hir::MutImmutable => AutoBorrowMutability::Immutable,
hir::MutMutable => AutoBorrowMutability::Mutable {
// For initial two-phase borrow
// deployment, conservatively omit
// overloaded function call ops.
allow_two_phase_borrow: AllowTwoPhase::No,
}
};
autoref = Some(Adjustment {
kind: Adjust::Borrow(AutoBorrow::Ref(region, mutbl)),
target: method.sig.inputs()[0]
});
}
if let Some(ok) = self.lookup_method_in_trait(call_expr.span,
method_name,
trait_def_id,
adjusted_ty,
None) {
let method = self.register_infer_ok_obligations(ok);
let mut autoref = None;
if borrow {
if let ty::Ref(region, _, mutbl) = method.sig.inputs()[0].sty {
let mutbl = match mutbl {
hir::MutImmutable => AutoBorrowMutability::Immutable,
hir::MutMutable => AutoBorrowMutability::Mutable {
// For initial two-phase borrow
// deployment, conservatively omit
// overloaded function call ops.
allow_two_phase_borrow: AllowTwoPhase::No,
}
};
autoref = Some(Adjustment {
kind: Adjust::Borrow(AutoBorrow::Ref(region, mutbl)),
target: method.sig.inputs()[0]
});
}
return Some((autoref, method));
}
return Some((autoref, method));
}
}

Expand Down Expand Up @@ -238,7 +235,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
err.span_suggestion_with_applicability(
call_expr.span,
&format!("`{}` is a unit variant, you need to write it \
without the parenthesis", path),
without the parenthesis", path),
path.to_string(),
Applicability::MachineApplicable
);
Expand Down
22 changes: 11 additions & 11 deletions src/librustc_typeck/check/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,11 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
let cast_ty = fcx.ty_to_string(self.cast_ty);
err.span_label(error_span,
format!("cannot cast `{}` as `{}`",
fcx.ty_to_string(self.expr_ty),
cast_ty));
fcx.ty_to_string(self.expr_ty),
cast_ty));
if let Ok(snippet) = fcx.sess().source_map().span_to_snippet(self.expr.span) {
err.span_help(self.expr.span,
&format!("did you mean `*{}`?", snippet));
&format!("did you mean `*{}`?", snippet));
}
err.emit();
}
Expand Down Expand Up @@ -267,16 +267,16 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
}
CastError::CastToChar => {
type_error_struct!(fcx.tcx.sess, self.span, self.expr_ty, E0604,
"only `u8` can be cast as `char`, not `{}`", self.expr_ty).emit();
"only `u8` can be cast as `char`, not `{}`", self.expr_ty).emit();
}
CastError::NonScalar => {
type_error_struct!(fcx.tcx.sess, self.span, self.expr_ty, E0605,
"non-primitive cast: `{}` as `{}`",
self.expr_ty,
fcx.ty_to_string(self.cast_ty))
.note("an `as` expression can only be used to convert between \
primitive types. Consider using the `From` trait")
.emit();
"non-primitive cast: `{}` as `{}`",
self.expr_ty,
fcx.ty_to_string(self.cast_ty))
.note("an `as` expression can only be used to convert between \
primitive types. Consider using the `From` trait")
.emit();
}
CastError::SizedUnsizedCast => {
use structured_errors::{SizedUnsizedCastError, StructuredDiagnostic};
Expand Down Expand Up @@ -445,7 +445,7 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
self.expr_ty,
fcx.tcx.mk_fn_ptr(f),
AllowTwoPhase::No);
if !res.is_ok() {
if res.is_err() {
return Err(CastError::NonScalar);
}
(FnPtr, t_cast)
Expand Down
38 changes: 16 additions & 22 deletions src/librustc_typeck/check/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,20 +231,19 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
obligation.predicate
);

match obligation.predicate {
if let ty::Predicate::Projection(ref proj_predicate) = obligation.predicate {
// Given a Projection predicate, we can potentially infer
// the complete signature.
ty::Predicate::Projection(ref proj_predicate) => {
let trait_ref = proj_predicate.to_poly_trait_ref(self.tcx);
self.self_type_matches_expected_vid(trait_ref, expected_vid)
.and_then(|_| {
self.deduce_sig_from_projection(
Some(obligation.cause.span),
proj_predicate,
)
})
}
_ => None,
let trait_ref = proj_predicate.to_poly_trait_ref(self.tcx);
self.self_type_matches_expected_vid(trait_ref, expected_vid)
.and_then(|_| {
self.deduce_sig_from_projection(
Some(obligation.cause.span),
proj_predicate
)
})
} else {
None
}
})
.next();
Expand Down Expand Up @@ -318,9 +317,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {

let input_tys = match arg_param_ty.sty {
ty::Tuple(tys) => tys.into_iter(),
_ => {
return None;
}
_ => return None
};

let ret_param_ty = projection.skip_binder().ty;
Expand Down Expand Up @@ -560,8 +557,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
// The liberated version of this signature should be be a subtype
// of the liberated form of the expectation.
for ((hir_ty, &supplied_ty), expected_ty) in decl.inputs.iter()
.zip(*supplied_sig.inputs().skip_binder()) // binder moved to (*) below
.zip(expected_sigs.liberated_sig.inputs())
.zip(*supplied_sig.inputs().skip_binder()) // binder moved to (*) below
.zip(expected_sigs.liberated_sig.inputs())
// `liberated_sig` is E'.
{
// Instantiate (this part of..) S to S', i.e., with fresh variables.
Expand Down Expand Up @@ -638,11 +635,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
self.tcx.types.err
});

match decl.output {
hir::Return(ref output) => {
astconv.ast_ty_to_ty(&output);
}
hir::DefaultReturn(_) => {}
if let hir::Return(ref output) = decl.output {
astconv.ast_ty_to_ty(&output);
}

let result = ty::Binder::bind(self.tcx.mk_fn_sig(
Expand Down
37 changes: 15 additions & 22 deletions src/librustc_typeck/check/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
fn unify(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> InferResult<'tcx, Ty<'tcx>> {
self.commit_if_ok(|_| {
if self.use_lub {
self.at(&self.cause, self.fcx.param_env)
.lub(b, a)
self.at(&self.cause, self.fcx.param_env).lub(b, a)
} else {
self.at(&self.cause, self.fcx.param_env)
.sup(b, a)
Expand Down Expand Up @@ -256,8 +255,8 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
b: Ty<'tcx>,
r_b: ty::Region<'tcx>,
mt_b: TypeAndMut<'tcx>)
-> CoerceResult<'tcx> {

-> CoerceResult<'tcx>
{
debug!("coerce_borrowed_pointer(a={:?}, b={:?})", a, b);

// If we have a parameter of type `&M T_a` and the value
Expand Down Expand Up @@ -591,9 +590,7 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
}

Ok(Some(vtable)) => {
for obligation in vtable.nested_obligations() {
queue.push_back(obligation);
}
queue.extend(vtable.nested_obligations())
}
}
}
Expand All @@ -620,12 +617,11 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
G: FnOnce(Ty<'tcx>) -> Vec<Adjustment<'tcx>>
{
if let ty::FnPtr(fn_ty_b) = b.sty {
match (fn_ty_a.unsafety(), fn_ty_b.unsafety()) {
(hir::Unsafety::Normal, hir::Unsafety::Unsafe) => {
let unsafe_a = self.tcx.safe_to_unsafe_fn_ty(fn_ty_a);
return self.unify_and(unsafe_a, b, to_unsafe);
}
_ => {}
if let (hir::Unsafety::Normal, hir::Unsafety::Unsafe)
= (fn_ty_a.unsafety(), fn_ty_b.unsafety())
{
let unsafe_a = self.tcx.safe_to_unsafe_fn_ty(fn_ty_a);
return self.unify_and(unsafe_a, b, to_unsafe);
}
}
self.unify_and(a, b, normal)
Expand Down Expand Up @@ -653,7 +649,6 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
-> CoerceResult<'tcx> {
//! Attempts to coerce from the type of a Rust function item
//! into a closure or a `proc`.
//!

let b = self.shallow_resolve(b);
debug!("coerce_from_fn_item(a={:?}, b={:?})", a, b);
Expand Down Expand Up @@ -724,9 +719,7 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
let (is_ref, mt_a) = match a.sty {
ty::Ref(_, ty, mutbl) => (true, ty::TypeAndMut { ty, mutbl }),
ty::RawPtr(mt) => (false, mt),
_ => {
return self.unify_and(a, b, identity);
}
_ => return self.unify_and(a, b, identity)
};

// Check that the types which they point at are compatible.
Expand Down Expand Up @@ -896,10 +889,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
};

if !noop {
return self.commit_if_ok(|_| {
return self.commit_if_ok(|_|
self.at(cause, self.param_env)
.lub(prev_ty, new_ty)
}).map(|ok| self.register_infer_ok_obligations(ok));
).map(|ok| self.register_infer_ok_obligations(ok));
}
}

Expand All @@ -909,10 +902,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
if let Some(e) = first_error {
Err(e)
} else {
self.commit_if_ok(|_| {
self.commit_if_ok(|_|
self.at(cause, self.param_env)
.lub(prev_ty, new_ty)
}).map(|ok| self.register_infer_ok_obligations(ok))
).map(|ok| self.register_infer_ok_obligations(ok))
}
}
Ok(ok) => {
Expand Down Expand Up @@ -1005,7 +998,7 @@ impl<'gcx, 'tcx, 'exprs, E> CoerceMany<'gcx, 'tcx, 'exprs, E>
/// needlessly cloning the slice.
pub fn with_coercion_sites(expected_ty: Ty<'tcx>,
coercion_sites: &'exprs [E])
-> Self {
-> Self {
Self::make(expected_ty, Expressions::UpFront(coercion_sites))
}

Expand Down
Loading

0 comments on commit f1694ea

Please sign in to comment.