Skip to content

Commit

Permalink
typeck/expr.rs: extract out check_expr_array.
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Jun 15, 2019
1 parent fe004da commit 877d834
Showing 1 changed file with 38 additions and 29 deletions.
67 changes: 38 additions & 29 deletions src/librustc_typeck/check/expr.rs
Expand Up @@ -125,35 +125,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.check_expr_with_expectation(e, expected)
}
ExprKind::Array(ref args) => {
let uty = expected.to_option(self).and_then(|uty| {
match uty.sty {
ty::Array(ty, _) | ty::Slice(ty) => Some(ty),
_ => None
}
});

let element_ty = if !args.is_empty() {
let coerce_to = uty.unwrap_or_else(|| {
self.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::TypeInference,
span: expr.span,
})
});
let mut coerce = CoerceMany::with_coercion_sites(coerce_to, args);
assert_eq!(self.diverges.get(), Diverges::Maybe);
for e in args {
let e_ty = self.check_expr_with_hint(e, coerce_to);
let cause = self.misc(e.span);
coerce.coerce(self, &cause, e, e_ty);
}
coerce.complete(self)
} else {
self.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::TypeInference,
span: expr.span,
})
};
tcx.mk_array(element_ty, args.len() as u64)
self.check_expr_array(args, expected, expr)
}
ExprKind::Repeat(ref element, ref count) => {
let count_def_id = tcx.hir().local_def_id_from_hir_id(count.hir_id);
Expand Down Expand Up @@ -815,4 +787,41 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}
}

fn check_expr_array(
&self,
args: &'tcx [hir::Expr],
expected: Expectation<'tcx>,
expr: &'tcx hir::Expr
) -> Ty<'tcx> {
let uty = expected.to_option(self).and_then(|uty| {
match uty.sty {
ty::Array(ty, _) | ty::Slice(ty) => Some(ty),
_ => None
}
});

let element_ty = if !args.is_empty() {
let coerce_to = uty.unwrap_or_else(|| {
self.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::TypeInference,
span: expr.span,
})
});
let mut coerce = CoerceMany::with_coercion_sites(coerce_to, args);
assert_eq!(self.diverges.get(), Diverges::Maybe);
for e in args {
let e_ty = self.check_expr_with_hint(e, coerce_to);
let cause = self.misc(e.span);
coerce.coerce(self, &cause, e, e_ty);
}
coerce.complete(self)
} else {
self.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::TypeInference,
span: expr.span,
})
};
self.tcx.mk_array(element_ty, args.len() as u64)
}
}

0 comments on commit 877d834

Please sign in to comment.