Skip to content

Commit

Permalink
Refactor ty_infer and re_infer
Browse files Browse the repository at this point in the history
  • Loading branch information
varkor committed Jun 7, 2019
1 parent 5377dea commit a0e3e9a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 38 deletions.
31 changes: 15 additions & 16 deletions src/librustc_typeck/astconv.rs
Expand Up @@ -49,19 +49,17 @@ pub trait AstConv<'gcx, 'tcx> {
-> &'tcx ty::GenericPredicates<'tcx>;

/// Returns the lifetime to use when a lifetime is omitted (and not elided).
fn re_infer(&self, span: Span, _def: Option<&ty::GenericParamDef>)
fn re_infer(
&self,
param: Option<&ty::GenericParamDef>,
span: Span,
)
-> Option<ty::Region<'tcx>>;

/// Returns the type to use when a type is omitted.
fn ty_infer(&self, span: Span) -> Ty<'tcx>;
fn ty_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx>;

/// Same as `ty_infer`, but with a known type parameter definition.
fn ty_infer_for_def(&self,
_def: &ty::GenericParamDef,
span: Span) -> Ty<'tcx> {
self.ty_infer(span)
}
/// What const should we use when a const is omitted?
/// Returns the const to use when a const is omitted.
fn ct_infer(
&self,
ty: Ty<'tcx>,
Expand Down Expand Up @@ -163,7 +161,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
}

None => {
self.re_infer(lifetime.span, def)
self.re_infer(def, lifetime.span)
.unwrap_or_else(|| {
// This indicates an illegal lifetime
// elision. `resolve_lifetime` should have
Expand Down Expand Up @@ -701,11 +699,12 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
}
} else if infer_args {
// No type parameters were provided, we can infer all.
if !default_needs_object_self(param) {
self.ty_infer_for_def(param, span).into()
let param = if !default_needs_object_self(param) {
Some(param)
} else {
self.ty_infer(span).into()
}
None
};
self.ty_infer(param, span).into()
} else {
// We've already errored above about the mismatch.
tcx.types.err.into()
Expand Down Expand Up @@ -1440,7 +1439,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
if tcx.named_region(lifetime.hir_id).is_some() {
self.ast_region_to_region(lifetime, None)
} else {
self.re_infer(span, None).unwrap_or_else(|| {
self.re_infer(None, span).unwrap_or_else(|| {
span_err!(tcx.sess, span, E0228,
"the lifetime bound for this object type cannot be deduced \
from context; please supply an explicit bound");
Expand Down Expand Up @@ -2134,7 +2133,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
// values in a ExprKind::Closure, or as
// the type of local variables. Both of these cases are
// handled specially and will not descend into this routine.
self.ty_infer(ast_ty.span)
self.ty_infer(None, ast_ty.span)
}
hir::TyKind::CVarArgs(lt) => {
let va_list_did = match tcx.lang_items().va_list() {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/closure.rs
Expand Up @@ -598,7 +598,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let supplied_arguments = decl.inputs.iter().map(|a| astconv.ast_ty_to_ty(a));
let supplied_return = match decl.output {
hir::Return(ref output) => astconv.ast_ty_to_ty(&output),
hir::DefaultReturn(_) => astconv.ty_infer(decl.output.span()),
hir::DefaultReturn(_) => astconv.ty_infer(None, decl.output.span()),
};

let result = ty::Binder::bind(self.tcx.mk_fn_sig(
Expand Down
32 changes: 18 additions & 14 deletions src/librustc_typeck/check/mod.rs
Expand Up @@ -1939,27 +1939,32 @@ impl<'a, 'gcx, 'tcx> AstConv<'gcx, 'tcx> for FnCtxt<'a, 'gcx, 'tcx> {
})
}

fn re_infer(&self, span: Span, def: Option<&ty::GenericParamDef>)
-> Option<ty::Region<'tcx>> {
fn re_infer(
&self,
def: Option<&ty::GenericParamDef>,
span: Span,
) -> Option<ty::Region<'tcx>> {
let v = match def {
Some(def) => infer::EarlyBoundRegion(span, def.name),
None => infer::MiscVariable(span)
};
Some(self.next_region_var(v))
}

fn ty_infer(&self, span: Span) -> Ty<'tcx> {
self.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::TypeInference,
span,
})
fn ty_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> {
if let Some(param) = param {
if let UnpackedKind::Type(ty) = self.var_for_def(span, param).unpack() {
return ty;
}
unreachable!()
} else {
self.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::TypeInference,
span,
})
}
}

fn ty_infer_for_def(&self,
ty_param_def: &ty::GenericParamDef,
span: Span) -> Ty<'tcx> {
if let UnpackedKind::Type(ty) = self.var_for_def(span, ty_param_def).unpack() {
return ty;
fn ct_infer(
&self,
ty: Ty<'tcx>,
Expand All @@ -1977,7 +1982,6 @@ impl<'a, 'gcx, 'tcx> AstConv<'gcx, 'tcx> for FnCtxt<'a, 'gcx, 'tcx> {
span,
})
}
unreachable!()
}

fn projected_ty_from_poly_trait_ref(&self,
Expand Down Expand Up @@ -5463,7 +5467,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|substs, param, infer_args| {
match param.kind {
GenericParamDefKind::Lifetime => {
self.re_infer(span, Some(param)).unwrap().into()
self.re_infer(Some(param), span).unwrap().into()
}
GenericParamDefKind::Type { has_default, .. } => {
if !infer_args && has_default {
Expand Down
13 changes: 6 additions & 7 deletions src/librustc_typeck/collect.rs
Expand Up @@ -186,18 +186,17 @@ impl<'a, 'tcx> AstConv<'tcx, 'tcx> for ItemCtxt<'a, 'tcx> {

fn re_infer(
&self,
_span: Span,
_def: Option<&ty::GenericParamDef>,
_: Option<&ty::GenericParamDef>,
_: Span,
) -> Option<ty::Region<'tcx>> {
None
}

fn ty_infer(&self, span: Span) -> Ty<'tcx> {
struct_span_err!(
self.tcx().sess,
fn ty_infer(&self, _: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> {
self.tcx().sess.struct_span_err_with_code(
span,
E0121,
"the type placeholder `_` is not allowed within types on item signatures"
"the type placeholder `_` is not allowed within types on item signatures",
DiagnosticId::Error("E0121".into()),
).span_label(span, "not allowed in type signatures")
.emit();

Expand Down

0 comments on commit a0e3e9a

Please sign in to comment.