use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
use rustc::hir::itemlikevisit::ItemLikeVisitor;
+use rustc::hir::ptr::P;
use crate::middle::lang_items;
use crate::namespace::Namespace;
use rustc::infer::{self, InferCtxt, InferOk, InferResult};
use syntax::ast;
use syntax::attr;
use syntax::feature_gate::{GateIssue, emit_feature_err};
-use syntax::ptr::P;
use syntax::source_map::{DUMMY_SP, original_sp};
use syntax::symbol::{kw, sym};
/// checking this function. On exit, if we find that *more* errors
/// have been reported, we will skip regionck and other work that
/// expects the types within the function to be consistent.
+ // FIXME(matthewjasper) This should not exist, and it's not correct
+ // if type checking is run in parallel.
err_count_on_creation: usize,
ret_coercion: Option<RefCell<DynamicCoerceMany<'tcx>>>,
fn visit_impl_item(&mut self, _: &'tcx hir::ImplItem) { }
}
-pub fn check_wf_new<'tcx>(tcx: TyCtxt<'tcx>) -> Result<(), ErrorReported> {
- tcx.sess.track_errors(|| {
- let mut visit = wfcheck::CheckTypeWellFormedVisitor::new(tcx);
- tcx.hir().krate().par_visit_all_item_likes(&mut visit);
- })
+pub fn check_wf_new(tcx: TyCtxt<'_>) {
+ let mut visit = wfcheck::CheckTypeWellFormedVisitor::new(tcx);
+ tcx.hir().krate().par_visit_all_item_likes(&mut visit);
}
-fn check_mod_item_types<'tcx>(tcx: TyCtxt<'tcx>, module_def_id: DefId) {
+fn check_mod_item_types(tcx: TyCtxt<'_>, module_def_id: DefId) {
tcx.hir().visit_item_likes_in_module(module_def_id, &mut CheckItemTypesVisitor { tcx });
}
-fn typeck_item_bodies<'tcx>(tcx: TyCtxt<'tcx>, crate_num: CrateNum) {
+fn typeck_item_bodies(tcx: TyCtxt<'_>, crate_num: CrateNum) {
debug_assert!(crate_num == LOCAL_CRATE);
tcx.par_body_owners(|body_owner_def_id| {
tcx.ensure().typeck_tables_of(body_owner_def_id);
});
}
-fn check_item_well_formed<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) {
+fn check_item_well_formed(tcx: TyCtxt<'_>, def_id: DefId) {
wfcheck::check_item_well_formed(tcx, def_id);
}
-fn check_trait_item_well_formed<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) {
+fn check_trait_item_well_formed(tcx: TyCtxt<'_>, def_id: DefId) {
wfcheck::check_trait_item(tcx, def_id);
}
-fn check_impl_item_well_formed<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) {
+fn check_impl_item_well_formed(tcx: TyCtxt<'_>, def_id: DefId) {
wfcheck::check_impl_item(tcx, def_id);
}
};
}
-fn adt_destructor<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Option<ty::Destructor> {
+fn adt_destructor(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ty::Destructor> {
tcx.calculate_dtor(def_id, &mut dropck::check_drop_impl)
}
/// may not succeed. In some cases where this function returns `None`
/// (notably closures), `typeck_tables(def_id)` would wind up
/// redirecting to the owning function.
-fn primary_body_of<'tcx>(
- tcx: TyCtxt<'tcx>,
+fn primary_body_of(
+ tcx: TyCtxt<'_>,
id: hir::HirId,
-) -> Option<(hir::BodyId, Option<&'tcx hir::FnDecl>)> {
- match tcx.hir().get_by_hir_id(id) {
+) -> Option<(hir::BodyId, Option<&hir::FnDecl>)> {
+ match tcx.hir().get(id) {
Node::Item(item) => {
match item.node {
hir::ItemKind::Const(_, body) |
}
}
-fn has_typeck_tables<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
+fn has_typeck_tables(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
// Closures' tables come from their outermost function,
// as they are part of the same "inference environment".
let outer_def_id = tcx.closure_base_def_id(def_id);
primary_body_of(tcx, id).is_some()
}
-fn used_trait_imports<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx DefIdSet {
+fn used_trait_imports(tcx: TyCtxt<'_>, def_id: DefId) -> &DefIdSet {
&*tcx.typeck_tables_of(def_id).used_trait_imports
}
-fn typeck_tables_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx ty::TypeckTables<'tcx> {
+fn typeck_tables_of(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::TypeckTables<'_> {
// Closures' tables come from their outermost function,
// as they are part of the same "inference environment".
let outer_def_id = tcx.closure_base_def_id(def_id);
let revealed_ty = if tcx.features().impl_trait_in_bindings {
fcx.instantiate_opaque_types_from_value(
id,
- &expected_type
+ &expected_type,
+ body.value.span,
)
} else {
expected_type
tables
}
-fn check_abi<'tcx>(tcx: TyCtxt<'tcx>, span: Span, abi: Abi) {
+fn check_abi(tcx: TyCtxt<'_>, span: Span, abi: Abi) {
if !tcx.sess.target.target.is_abi_supported(abi) {
struct_span_err!(tcx.sess, span, E0570,
"The ABI `{}` is not supported for the current target", abi).emit()
let revealed_ty = if self.fcx.tcx.features().impl_trait_in_bindings {
self.fcx.instantiate_opaque_types_from_value(
self.parent_id,
- &o_ty
+ &o_ty,
+ ty.span,
)
} else {
o_ty
if let PatKind::Binding(_, _, ident, _) = p.node {
let var_ty = self.assign(p.span, p.hir_id, None);
- let node_id = self.fcx.tcx.hir().hir_to_node_id(p.hir_id);
if !self.fcx.tcx.features().unsized_locals {
self.fcx.require_type_is_sized(var_ty, p.span,
- traits::VariableType(node_id));
+ traits::VariableType(p.hir_id));
}
debug!("Pattern binding {} is assigned to {} with type {:?}",
let declared_ret_ty = fn_sig.output();
fcx.require_type_is_sized(declared_ret_ty, decl.output.span(), traits::SizedReturnType);
- let revealed_ret_ty = fcx.instantiate_opaque_types_from_value(fn_id, &declared_ret_ty);
+ let revealed_ret_ty = fcx.instantiate_opaque_types_from_value(
+ fn_id,
+ &declared_ret_ty,
+ decl.output.span(),
+ );
fcx.ret_coercion = Some(RefCell::new(CoerceMany::new(revealed_ret_ty)));
fn_sig = fcx.tcx.mk_fn_sig(
fn_sig.inputs().iter().cloned(),
);
}
- if let Node::Item(item) = fcx.tcx.hir().get_by_hir_id(fn_id) {
+ if let Node::Item(item) = fcx.tcx.hir().get(fn_id) {
if let ItemKind::Fn(_, _, ref generics, _) = item.node {
if !generics.params.is_empty() {
fcx.tcx.sess.span_err(
);
}
- if let Node::Item(item) = fcx.tcx.hir().get_by_hir_id(fn_id) {
+ if let Node::Item(item) = fcx.tcx.hir().get(fn_id) {
if let ItemKind::Fn(_, _, ref generics, _) = item.node {
if !generics.params.is_empty() {
fcx.tcx.sess.span_err(
(fcx, gen_ty)
}
-fn check_struct<'tcx>(tcx: TyCtxt<'tcx>, id: hir::HirId, span: Span) {
+fn check_struct(tcx: TyCtxt<'_>, id: hir::HirId, span: Span) {
let def_id = tcx.hir().local_def_id_from_hir_id(id);
let def = tcx.adt_def(def_id);
def.destructor(tcx); // force the destructor to be evaluated
check_packed(tcx, span, def_id);
}
-fn check_union<'tcx>(tcx: TyCtxt<'tcx>, id: hir::HirId, span: Span) {
+fn check_union(tcx: TyCtxt<'_>, id: hir::HirId, span: Span) {
let def_id = tcx.hir().local_def_id_from_hir_id(id);
let def = tcx.adt_def(def_id);
def.destructor(tcx); // force the destructor to be evaluated
}
}
-fn check_on_unimplemented<'tcx>(tcx: TyCtxt<'tcx>, trait_def_id: DefId, item: &hir::Item) {
+fn check_on_unimplemented(tcx: TyCtxt<'_>, trait_def_id: DefId, item: &hir::Item) {
let item_def_id = tcx.hir().local_def_id_from_hir_id(item.hir_id);
// an error would be reported if this fails.
let _ = traits::OnUnimplementedDirective::of_item(tcx, trait_def_id, item_def_id);
}
-fn report_forbidden_specialization<'tcx>(
- tcx: TyCtxt<'tcx>,
+fn report_forbidden_specialization(
+ tcx: TyCtxt<'_>,
impl_item: &hir::ImplItem,
parent_impl: DefId,
) {
/// Checks whether a type can be represented in memory. In particular, it
/// identifies types that contain themselves without indirection through a
/// pointer, which would mean their size is unbounded.
-fn check_representable<'tcx>(tcx: TyCtxt<'tcx>, sp: Span, item_def_id: DefId) -> bool {
+fn check_representable(tcx: TyCtxt<'_>, sp: Span, item_def_id: DefId) -> bool {
let rty = tcx.type_of(item_def_id);
// Check that it is possible to represent this type. This call identifies
return true;
}
-pub fn check_simd<'tcx>(tcx: TyCtxt<'tcx>, sp: Span, def_id: DefId) {
+pub fn check_simd(tcx: TyCtxt<'_>, sp: Span, def_id: DefId) {
let t = tcx.type_of(def_id);
if let ty::Adt(def, substs) = t.sty {
if def.is_struct() {
}
}
-fn check_packed<'tcx>(tcx: TyCtxt<'tcx>, sp: Span, def_id: DefId) {
+fn check_packed(tcx: TyCtxt<'_>, sp: Span, def_id: DefId) {
let repr = tcx.adt_def(def_id).repr;
if repr.packed() {
for attr in tcx.get_attrs(def_id).iter() {
}
}
-fn check_packed_inner<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, stack: &mut Vec<DefId>) -> bool {
+fn check_packed_inner(tcx: TyCtxt<'_>, def_id: DefId, stack: &mut Vec<DefId>) -> bool {
let t = tcx.type_of(def_id);
if stack.contains(&def_id) {
debug!("check_packed_inner: {:?} is recursive", t);
err.emit();
}
-fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, sp: Span, def_id: DefId) {
+fn check_transparent(tcx: TyCtxt<'_>, sp: Span, def_id: DefId) {
let adt = tcx.adt_def(def_id);
if !adt.repr.transparent() {
return;
}
}
+ if tcx.adt_def(def_id).repr.int.is_none() && tcx.features().arbitrary_enum_discriminant {
+ let is_unit =
+ |var: &hir::Variant| match var.node.data {
+ hir::VariantData::Unit(..) => true,
+ _ => false
+ };
+
+ let has_disr = |var: &hir::Variant| var.node.disr_expr.is_some();
+ let has_non_units = vs.iter().any(|var| !is_unit(var));
+ let disr_units = vs.iter().any(|var| is_unit(&var) && has_disr(&var));
+ let disr_non_unit = vs.iter().any(|var| !is_unit(&var) && has_disr(&var));
+
+ if disr_non_unit || (disr_units && has_non_units) {
+ let mut err = struct_span_err!(tcx.sess, sp, E0732,
+ "`#[repr(inttype)]` must be specified");
+ err.emit();
+ }
+ }
+
let mut disr_vals: Vec<Discr<'tcx>> = Vec::with_capacity(vs.len());
for ((_, discr), v) in def.discriminants(tcx).zip(vs) {
// Check for duplicate discriminant values
check_transparent(tcx, sp, def_id);
}
-fn report_unexpected_variant_res<'tcx>(tcx: TyCtxt<'tcx>, res: Res, span: Span, qpath: &QPath) {
+fn report_unexpected_variant_res(tcx: TyCtxt<'_>, res: Res, span: Span, qpath: &QPath) {
span_err!(tcx.sess, span, E0533,
"expected unit struct/variant or constant, found {} `{}`",
res.descr(),
&self.tcx.sess
}
- pub fn err_count_since_creation(&self) -> usize {
- self.tcx.sess.err_count() - self.err_count_on_creation
+ pub fn errors_reported_since_creation(&self) -> bool {
+ self.tcx.sess.err_count() > self.err_count_on_creation
}
/// Produces warning on the given node, if the current point in the
&self,
parent_id: hir::HirId,
value: &T,
+ value_span: Span,
) -> T {
let parent_def_id = self.tcx.hir().local_def_id_from_hir_id(parent_id);
debug!("instantiate_opaque_types_from_value(parent_def_id={:?}, value={:?})",
self.body_id,
self.param_env,
value,
+ value_span,
)
);
}
fn parent_item_span(&self, id: hir::HirId) -> Option<Span> {
- let node = self.tcx.hir().get_by_hir_id(self.tcx.hir().get_parent_item(id));
+ let node = self.tcx.hir().get(self.tcx.hir().get_parent_item(id));
match node {
Node::Item(&hir::Item {
node: hir::ItemKind::Fn(_, _, _, body_id), ..
/// Given a function block's `HirId`, returns its `FnDecl` if it exists, or `None` otherwise.
fn get_parent_fn_decl(&self, blk_id: hir::HirId) -> Option<(&'tcx hir::FnDecl, ast::Ident)> {
- let parent = self.tcx.hir().get_by_hir_id(self.tcx.hir().get_parent_item(blk_id));
+ let parent = self.tcx.hir().get(self.tcx.hir().get_parent_item(blk_id));
self.get_node_fn_decl(parent).map(|(fn_decl, ident, _)| (fn_decl, ident))
}
// Get enclosing Fn, if it is a function or a trait method, unless there's a `loop` or
// `while` before reaching it, as block tail returns are not available in them.
self.tcx.hir().get_return_block(blk_id).and_then(|blk_id| {
- let parent = self.tcx.hir().get_by_hir_id(blk_id);
+ let parent = self.tcx.hir().get(blk_id);
self.get_node_fn_decl(parent).map(|(fn_decl, _, is_main)| (fn_decl, is_main))
})
}
}
}
+ /// A possible error is to forget to add `.await` when using futures:
+ ///
+ /// ```
+ /// #![feature(async_await)]
+ ///
+ /// async fn make_u32() -> u32 {
+ /// 22
+ /// }
+ ///
+ /// fn take_u32(x: u32) {}
+ ///
+ /// async fn foo() {
+ /// let x = make_u32();
+ /// take_u32(x);
+ /// }
+ /// ```
+ ///
+ /// This routine checks if the found type `T` implements `Future<Output=U>` where `U` is the
+ /// expected type. If this is the case, and we are inside of an async body, it suggests adding
+ /// `.await` to the tail of the expression.
+ fn suggest_missing_await(
+ &self,
+ err: &mut DiagnosticBuilder<'tcx>,
+ expr: &hir::Expr,
+ expected: Ty<'tcx>,
+ found: Ty<'tcx>,
+ ) {
+ // `.await` is not permitted outside of `async` bodies, so don't bother to suggest if the
+ // body isn't `async`.
+ let item_id = self.tcx().hir().get_parent_node(self.body_id);
+ if let Some(body_id) = self.tcx().hir().maybe_body_owned_by(item_id) {
+ let body = self.tcx().hir().body(body_id);
+ if let Some(hir::GeneratorKind::Async) = body.generator_kind {
+ let sp = expr.span;
+ // Check for `Future` implementations by constructing a predicate to
+ // prove: `<T as Future>::Output == U`
+ let future_trait = self.tcx.lang_items().future_trait().unwrap();
+ let item_def_id = self.tcx.associated_items(future_trait).next().unwrap().def_id;
+ let predicate = ty::Predicate::Projection(ty::Binder::bind(ty::ProjectionPredicate {
+ // `<T as Future>::Output`
+ projection_ty: ty::ProjectionTy {
+ // `T`
+ substs: self.tcx.mk_substs_trait(
+ found,
+ self.fresh_substs_for_item(sp, item_def_id)
+ ),
+ // `Future::Output`
+ item_def_id,
+ },
+ ty: expected,
+ }));
+ let obligation = traits::Obligation::new(self.misc(sp), self.param_env, predicate);
+ if self.infcx.predicate_may_hold(&obligation) {
+ if let Ok(code) = self.sess().source_map().span_to_snippet(sp) {
+ err.span_suggestion(
+ sp,
+ "consider using `.await` here",
+ format!("{}.await", code),
+ Applicability::MaybeIncorrect,
+ );
+ }
+ }
+ }
+ }
+ }
+
/// A common error is to add an extra semicolon:
///
/// ```
// If our calling expression is indeed the function itself, we're good!
// If not, generate an error that this can only be called directly.
- if let Node::Expr(expr) = self.tcx.hir().get_by_hir_id(
- self.tcx.hir().get_parent_node_by_hir_id(hir_id))
+ if let Node::Expr(expr) = self.tcx.hir().get(
+ self.tcx.hir().get_parent_node(hir_id))
{
if let ExprKind::Call(ref callee, ..) = expr.node {
if callee.hir_id == hir_id {
let mut contained_in_place = false;
while let hir::Node::Expr(parent_expr) =
- self.tcx.hir().get_by_hir_id(self.tcx.hir().get_parent_node_by_hir_id(expr_id))
+ self.tcx.hir().get(self.tcx.hir().get_parent_node(expr_id))
{
match &parent_expr.node {
hir::ExprKind::Assign(lhs, ..) | hir::ExprKind::AssignOp(_, lhs, ..) => {
} else if let ty::Error = leaf_ty.sty {
// If there is already another error, do not emit
// an error for not using a type Parameter.
- assert!(tcx.sess.err_count() > 0);
+ assert!(tcx.sess.has_errors());
return;
}
}