Skip to content

Commit 3527276

Browse files
committed
A few cleanups and minor improvements to typeck
1 parent 4415195 commit 3527276

19 files changed

+450
-581
lines changed

src/librustc_typeck/check/_match.rs

+30-32
Original file line numberDiff line numberDiff line change
@@ -81,35 +81,33 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
8181
//
8282
// See the examples in `run-pass/match-defbm*.rs`.
8383
let mut pat_adjustments = vec![];
84-
expected = loop {
84+
while let ty::Ref(_, inner_ty, inner_mutability) = exp_ty.sty {
8585
debug!("inspecting {:?} with type {:?}", exp_ty, exp_ty.sty);
86-
match exp_ty.sty {
87-
ty::Ref(_, inner_ty, inner_mutability) => {
88-
debug!("current discriminant is Ref, inserting implicit deref");
89-
// Preserve the reference type. We'll need it later during HAIR lowering.
90-
pat_adjustments.push(exp_ty);
91-
92-
exp_ty = inner_ty;
93-
def_bm = match def_bm {
94-
// If default binding mode is by value, make it `ref` or `ref mut`
95-
// (depending on whether we observe `&` or `&mut`).
96-
ty::BindByValue(_) =>
97-
ty::BindByReference(inner_mutability),
98-
99-
// Once a `ref`, always a `ref`. This is because a `& &mut` can't mutate
100-
// the underlying value.
101-
ty::BindByReference(hir::Mutability::MutImmutable) =>
102-
ty::BindByReference(hir::Mutability::MutImmutable),
103-
104-
// When `ref mut`, stay a `ref mut` (on `&mut`) or downgrade to `ref`
105-
// (on `&`).
106-
ty::BindByReference(hir::Mutability::MutMutable) =>
107-
ty::BindByReference(inner_mutability),
108-
};
109-
},
110-
_ => break exp_ty,
111-
}
112-
};
86+
87+
debug!("current discriminant is Ref, inserting implicit deref");
88+
// Preserve the reference type. We'll need it later during HAIR lowering.
89+
pat_adjustments.push(exp_ty);
90+
91+
exp_ty = inner_ty;
92+
def_bm = match def_bm {
93+
// If default binding mode is by value, make it `ref` or `ref mut`
94+
// (depending on whether we observe `&` or `&mut`).
95+
ty::BindByValue(_) =>
96+
ty::BindByReference(inner_mutability),
97+
98+
// Once a `ref`, always a `ref`. This is because a `& &mut` can't mutate
99+
// the underlying value.
100+
ty::BindByReference(hir::Mutability::MutImmutable) =>
101+
ty::BindByReference(hir::Mutability::MutImmutable),
102+
103+
// When `ref mut`, stay a `ref mut` (on `&mut`) or downgrade to `ref`
104+
// (on `&`).
105+
ty::BindByReference(hir::Mutability::MutMutable) =>
106+
ty::BindByReference(inner_mutability),
107+
};
108+
}
109+
expected = exp_ty;
110+
113111
if pat_adjustments.len() > 0 {
114112
debug!("default binding mode is now {:?}", def_bm);
115113
self.inh.tables.borrow_mut()
@@ -153,7 +151,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
153151
if let ty::Ref(_, r_ty, _) = expected_ty.sty {
154152
if let ty::Slice(_) = r_ty.sty {
155153
pat_ty = tcx.mk_imm_ref(tcx.types.re_static,
156-
tcx.mk_slice(tcx.types.u8))
154+
tcx.mk_slice(tcx.types.u8))
157155
}
158156
}
159157
}
@@ -294,7 +292,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
294292

295293
let element_tys_iter = (0..max_len).map(|_| self.next_ty_var(
296294
// FIXME: MiscVariable for now, obtaining the span and name information
297-
// from all tuple elements isn't trivial.
295+
// from all tuple elements isn't trivial.
298296
TypeVariableOrigin::TypeInference(pat.span)));
299297
let element_tys = tcx.mk_type_list(element_tys_iter);
300298
let pat_ty = tcx.mk_ty(ty::Tuple(element_tys));
@@ -394,7 +392,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
394392
tcx.sess, pat.span, E0527,
395393
"pattern requires {} elements but array has {}",
396394
min_len, size)
397-
.span_label(pat.span, format!("expected {} elements",size))
395+
.span_label(pat.span, format!("expected {} elements", size))
398396
.emit();
399397
}
400398
(inner_ty, tcx.types.err)
@@ -857,7 +855,7 @@ https://doc.rust-lang.org/reference/types.html#trait-objects");
857855
subpats.len(), subpats_ending, def.kind_name(),
858856
variant.fields.len(), fields_ending)
859857
.span_label(pat.span, format!("expected {} field{}, found {}",
860-
variant.fields.len(), fields_ending, subpats.len()))
858+
variant.fields.len(), fields_ending, subpats.len()))
861859
.emit();
862860
on_error();
863861
return tcx.types.err;

src/librustc_typeck/check/callee.rs

+24-27
Original file line numberDiff line numberDiff line change
@@ -166,34 +166,31 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
166166
None => continue,
167167
};
168168

169-
match self.lookup_method_in_trait(call_expr.span,
170-
method_name,
171-
trait_def_id,
172-
adjusted_ty,
173-
None) {
174-
None => continue,
175-
Some(ok) => {
176-
let method = self.register_infer_ok_obligations(ok);
177-
let mut autoref = None;
178-
if borrow {
179-
if let ty::Ref(region, _, mutbl) = method.sig.inputs()[0].sty {
180-
let mutbl = match mutbl {
181-
hir::MutImmutable => AutoBorrowMutability::Immutable,
182-
hir::MutMutable => AutoBorrowMutability::Mutable {
183-
// For initial two-phase borrow
184-
// deployment, conservatively omit
185-
// overloaded function call ops.
186-
allow_two_phase_borrow: AllowTwoPhase::No,
187-
}
188-
};
189-
autoref = Some(Adjustment {
190-
kind: Adjust::Borrow(AutoBorrow::Ref(region, mutbl)),
191-
target: method.sig.inputs()[0]
192-
});
193-
}
169+
if let Some(ok) = self.lookup_method_in_trait(call_expr.span,
170+
method_name,
171+
trait_def_id,
172+
adjusted_ty,
173+
None) {
174+
let method = self.register_infer_ok_obligations(ok);
175+
let mut autoref = None;
176+
if borrow {
177+
if let ty::Ref(region, _, mutbl) = method.sig.inputs()[0].sty {
178+
let mutbl = match mutbl {
179+
hir::MutImmutable => AutoBorrowMutability::Immutable,
180+
hir::MutMutable => AutoBorrowMutability::Mutable {
181+
// For initial two-phase borrow
182+
// deployment, conservatively omit
183+
// overloaded function call ops.
184+
allow_two_phase_borrow: AllowTwoPhase::No,
185+
}
186+
};
187+
autoref = Some(Adjustment {
188+
kind: Adjust::Borrow(AutoBorrow::Ref(region, mutbl)),
189+
target: method.sig.inputs()[0]
190+
});
194191
}
195-
return Some((autoref, method));
196192
}
193+
return Some((autoref, method));
197194
}
198195
}
199196

@@ -238,7 +235,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
238235
err.span_suggestion_with_applicability(
239236
call_expr.span,
240237
&format!("`{}` is a unit variant, you need to write it \
241-
without the parenthesis", path),
238+
without the parenthesis", path),
242239
path.to_string(),
243240
Applicability::MachineApplicable
244241
);

src/librustc_typeck/check/cast.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,11 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
219219
let cast_ty = fcx.ty_to_string(self.cast_ty);
220220
err.span_label(error_span,
221221
format!("cannot cast `{}` as `{}`",
222-
fcx.ty_to_string(self.expr_ty),
223-
cast_ty));
222+
fcx.ty_to_string(self.expr_ty),
223+
cast_ty));
224224
if let Ok(snippet) = fcx.sess().source_map().span_to_snippet(self.expr.span) {
225225
err.span_help(self.expr.span,
226-
&format!("did you mean `*{}`?", snippet));
226+
&format!("did you mean `*{}`?", snippet));
227227
}
228228
err.emit();
229229
}
@@ -267,16 +267,16 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
267267
}
268268
CastError::CastToChar => {
269269
type_error_struct!(fcx.tcx.sess, self.span, self.expr_ty, E0604,
270-
"only `u8` can be cast as `char`, not `{}`", self.expr_ty).emit();
270+
"only `u8` can be cast as `char`, not `{}`", self.expr_ty).emit();
271271
}
272272
CastError::NonScalar => {
273273
type_error_struct!(fcx.tcx.sess, self.span, self.expr_ty, E0605,
274-
"non-primitive cast: `{}` as `{}`",
275-
self.expr_ty,
276-
fcx.ty_to_string(self.cast_ty))
277-
.note("an `as` expression can only be used to convert between \
278-
primitive types. Consider using the `From` trait")
279-
.emit();
274+
"non-primitive cast: `{}` as `{}`",
275+
self.expr_ty,
276+
fcx.ty_to_string(self.cast_ty))
277+
.note("an `as` expression can only be used to convert between \
278+
primitive types. Consider using the `From` trait")
279+
.emit();
280280
}
281281
CastError::SizedUnsizedCast => {
282282
use structured_errors::{SizedUnsizedCastError, StructuredDiagnostic};
@@ -445,7 +445,7 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
445445
self.expr_ty,
446446
fcx.tcx.mk_fn_ptr(f),
447447
AllowTwoPhase::No);
448-
if !res.is_ok() {
448+
if res.is_err() {
449449
return Err(CastError::NonScalar);
450450
}
451451
(FnPtr, t_cast)

src/librustc_typeck/check/closure.rs

+16-22
Original file line numberDiff line numberDiff line change
@@ -231,20 +231,19 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
231231
obligation.predicate
232232
);
233233

234-
match obligation.predicate {
234+
if let ty::Predicate::Projection(ref proj_predicate) = obligation.predicate {
235235
// Given a Projection predicate, we can potentially infer
236236
// the complete signature.
237-
ty::Predicate::Projection(ref proj_predicate) => {
238-
let trait_ref = proj_predicate.to_poly_trait_ref(self.tcx);
239-
self.self_type_matches_expected_vid(trait_ref, expected_vid)
240-
.and_then(|_| {
241-
self.deduce_sig_from_projection(
242-
Some(obligation.cause.span),
243-
proj_predicate,
244-
)
245-
})
246-
}
247-
_ => None,
237+
let trait_ref = proj_predicate.to_poly_trait_ref(self.tcx);
238+
self.self_type_matches_expected_vid(trait_ref, expected_vid)
239+
.and_then(|_| {
240+
self.deduce_sig_from_projection(
241+
Some(obligation.cause.span),
242+
proj_predicate
243+
)
244+
})
245+
} else {
246+
None
248247
}
249248
})
250249
.next();
@@ -318,9 +317,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
318317

319318
let input_tys = match arg_param_ty.sty {
320319
ty::Tuple(tys) => tys.into_iter(),
321-
_ => {
322-
return None;
323-
}
320+
_ => return None
324321
};
325322

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

641-
match decl.output {
642-
hir::Return(ref output) => {
643-
astconv.ast_ty_to_ty(&output);
644-
}
645-
hir::DefaultReturn(_) => {}
638+
if let hir::Return(ref output) = decl.output {
639+
astconv.ast_ty_to_ty(&output);
646640
}
647641

648642
let result = ty::Binder::bind(self.tcx.mk_fn_sig(

src/librustc_typeck/check/coercion.rs

+15-22
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
144144
fn unify(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> InferResult<'tcx, Ty<'tcx>> {
145145
self.commit_if_ok(|_| {
146146
if self.use_lub {
147-
self.at(&self.cause, self.fcx.param_env)
148-
.lub(b, a)
147+
self.at(&self.cause, self.fcx.param_env).lub(b, a)
149148
} else {
150149
self.at(&self.cause, self.fcx.param_env)
151150
.sup(b, a)
@@ -256,8 +255,8 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
256255
b: Ty<'tcx>,
257256
r_b: ty::Region<'tcx>,
258257
mt_b: TypeAndMut<'tcx>)
259-
-> CoerceResult<'tcx> {
260-
258+
-> CoerceResult<'tcx>
259+
{
261260
debug!("coerce_borrowed_pointer(a={:?}, b={:?})", a, b);
262261

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

593592
Ok(Some(vtable)) => {
594-
for obligation in vtable.nested_obligations() {
595-
queue.push_back(obligation);
596-
}
593+
queue.extend(vtable.nested_obligations())
597594
}
598595
}
599596
}
@@ -620,12 +617,11 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
620617
G: FnOnce(Ty<'tcx>) -> Vec<Adjustment<'tcx>>
621618
{
622619
if let ty::FnPtr(fn_ty_b) = b.sty {
623-
match (fn_ty_a.unsafety(), fn_ty_b.unsafety()) {
624-
(hir::Unsafety::Normal, hir::Unsafety::Unsafe) => {
625-
let unsafe_a = self.tcx.safe_to_unsafe_fn_ty(fn_ty_a);
626-
return self.unify_and(unsafe_a, b, to_unsafe);
627-
}
628-
_ => {}
620+
if let (hir::Unsafety::Normal, hir::Unsafety::Unsafe)
621+
= (fn_ty_a.unsafety(), fn_ty_b.unsafety())
622+
{
623+
let unsafe_a = self.tcx.safe_to_unsafe_fn_ty(fn_ty_a);
624+
return self.unify_and(unsafe_a, b, to_unsafe);
629625
}
630626
}
631627
self.unify_and(a, b, normal)
@@ -653,7 +649,6 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
653649
-> CoerceResult<'tcx> {
654650
//! Attempts to coerce from the type of a Rust function item
655651
//! into a closure or a `proc`.
656-
//!
657652
658653
let b = self.shallow_resolve(b);
659654
debug!("coerce_from_fn_item(a={:?}, b={:?})", a, b);
@@ -724,9 +719,7 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
724719
let (is_ref, mt_a) = match a.sty {
725720
ty::Ref(_, ty, mutbl) => (true, ty::TypeAndMut { ty, mutbl }),
726721
ty::RawPtr(mt) => (false, mt),
727-
_ => {
728-
return self.unify_and(a, b, identity);
729-
}
722+
_ => return self.unify_and(a, b, identity)
730723
};
731724

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

898891
if !noop {
899-
return self.commit_if_ok(|_| {
892+
return self.commit_if_ok(|_|
900893
self.at(cause, self.param_env)
901894
.lub(prev_ty, new_ty)
902-
}).map(|ok| self.register_infer_ok_obligations(ok));
895+
).map(|ok| self.register_infer_ok_obligations(ok));
903896
}
904897
}
905898

@@ -909,10 +902,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
909902
if let Some(e) = first_error {
910903
Err(e)
911904
} else {
912-
self.commit_if_ok(|_| {
905+
self.commit_if_ok(|_|
913906
self.at(cause, self.param_env)
914907
.lub(prev_ty, new_ty)
915-
}).map(|ok| self.register_infer_ok_obligations(ok))
908+
).map(|ok| self.register_infer_ok_obligations(ok))
916909
}
917910
}
918911
Ok(ok) => {
@@ -1005,7 +998,7 @@ impl<'gcx, 'tcx, 'exprs, E> CoerceMany<'gcx, 'tcx, 'exprs, E>
1005998
/// needlessly cloning the slice.
1006999
pub fn with_coercion_sites(expected_ty: Ty<'tcx>,
10071000
coercion_sites: &'exprs [E])
1008-
-> Self {
1001+
-> Self {
10091002
Self::make(expected_ty, Expressions::UpFront(coercion_sites))
10101003
}
10111004

0 commit comments

Comments
 (0)