Skip to content

Commit a0e3e9a

Browse files
committed
Refactor ty_infer and re_infer
1 parent 5377dea commit a0e3e9a

File tree

4 files changed

+40
-38
lines changed

4 files changed

+40
-38
lines changed

src/librustc_typeck/astconv.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,17 @@ pub trait AstConv<'gcx, 'tcx> {
4949
-> &'tcx ty::GenericPredicates<'tcx>;
5050

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

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

58-
/// Same as `ty_infer`, but with a known type parameter definition.
59-
fn ty_infer_for_def(&self,
60-
_def: &ty::GenericParamDef,
61-
span: Span) -> Ty<'tcx> {
62-
self.ty_infer(span)
63-
}
64-
/// What const should we use when a const is omitted?
62+
/// Returns the const to use when a const is omitted.
6563
fn ct_infer(
6664
&self,
6765
ty: Ty<'tcx>,
@@ -163,7 +161,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
163161
}
164162

165163
None => {
166-
self.re_infer(lifetime.span, def)
164+
self.re_infer(def, lifetime.span)
167165
.unwrap_or_else(|| {
168166
// This indicates an illegal lifetime
169167
// elision. `resolve_lifetime` should have
@@ -701,11 +699,12 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
701699
}
702700
} else if infer_args {
703701
// No type parameters were provided, we can infer all.
704-
if !default_needs_object_self(param) {
705-
self.ty_infer_for_def(param, span).into()
702+
let param = if !default_needs_object_self(param) {
703+
Some(param)
706704
} else {
707-
self.ty_infer(span).into()
708-
}
705+
None
706+
};
707+
self.ty_infer(param, span).into()
709708
} else {
710709
// We've already errored above about the mismatch.
711710
tcx.types.err.into()
@@ -1440,7 +1439,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
14401439
if tcx.named_region(lifetime.hir_id).is_some() {
14411440
self.ast_region_to_region(lifetime, None)
14421441
} else {
1443-
self.re_infer(span, None).unwrap_or_else(|| {
1442+
self.re_infer(None, span).unwrap_or_else(|| {
14441443
span_err!(tcx.sess, span, E0228,
14451444
"the lifetime bound for this object type cannot be deduced \
14461445
from context; please supply an explicit bound");
@@ -2134,7 +2133,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
21342133
// values in a ExprKind::Closure, or as
21352134
// the type of local variables. Both of these cases are
21362135
// handled specially and will not descend into this routine.
2137-
self.ty_infer(ast_ty.span)
2136+
self.ty_infer(None, ast_ty.span)
21382137
}
21392138
hir::TyKind::CVarArgs(lt) => {
21402139
let va_list_did = match tcx.lang_items().va_list() {

src/librustc_typeck/check/closure.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
598598
let supplied_arguments = decl.inputs.iter().map(|a| astconv.ast_ty_to_ty(a));
599599
let supplied_return = match decl.output {
600600
hir::Return(ref output) => astconv.ast_ty_to_ty(&output),
601-
hir::DefaultReturn(_) => astconv.ty_infer(decl.output.span()),
601+
hir::DefaultReturn(_) => astconv.ty_infer(None, decl.output.span()),
602602
};
603603

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

src/librustc_typeck/check/mod.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,27 +1939,32 @@ impl<'a, 'gcx, 'tcx> AstConv<'gcx, 'tcx> for FnCtxt<'a, 'gcx, 'tcx> {
19391939
})
19401940
}
19411941

1942-
fn re_infer(&self, span: Span, def: Option<&ty::GenericParamDef>)
1943-
-> Option<ty::Region<'tcx>> {
1942+
fn re_infer(
1943+
&self,
1944+
def: Option<&ty::GenericParamDef>,
1945+
span: Span,
1946+
) -> Option<ty::Region<'tcx>> {
19441947
let v = match def {
19451948
Some(def) => infer::EarlyBoundRegion(span, def.name),
19461949
None => infer::MiscVariable(span)
19471950
};
19481951
Some(self.next_region_var(v))
19491952
}
19501953

1951-
fn ty_infer(&self, span: Span) -> Ty<'tcx> {
1952-
self.next_ty_var(TypeVariableOrigin {
1953-
kind: TypeVariableOriginKind::TypeInference,
1954-
span,
1955-
})
1954+
fn ty_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> {
1955+
if let Some(param) = param {
1956+
if let UnpackedKind::Type(ty) = self.var_for_def(span, param).unpack() {
1957+
return ty;
1958+
}
1959+
unreachable!()
1960+
} else {
1961+
self.next_ty_var(TypeVariableOrigin {
1962+
kind: TypeVariableOriginKind::TypeInference,
1963+
span,
1964+
})
1965+
}
19561966
}
19571967

1958-
fn ty_infer_for_def(&self,
1959-
ty_param_def: &ty::GenericParamDef,
1960-
span: Span) -> Ty<'tcx> {
1961-
if let UnpackedKind::Type(ty) = self.var_for_def(span, ty_param_def).unpack() {
1962-
return ty;
19631968
fn ct_infer(
19641969
&self,
19651970
ty: Ty<'tcx>,
@@ -1977,7 +1982,6 @@ impl<'a, 'gcx, 'tcx> AstConv<'gcx, 'tcx> for FnCtxt<'a, 'gcx, 'tcx> {
19771982
span,
19781983
})
19791984
}
1980-
unreachable!()
19811985
}
19821986

19831987
fn projected_ty_from_poly_trait_ref(&self,
@@ -5463,7 +5467,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
54635467
|substs, param, infer_args| {
54645468
match param.kind {
54655469
GenericParamDefKind::Lifetime => {
5466-
self.re_infer(span, Some(param)).unwrap().into()
5470+
self.re_infer(Some(param), span).unwrap().into()
54675471
}
54685472
GenericParamDefKind::Type { has_default, .. } => {
54695473
if !infer_args && has_default {

src/librustc_typeck/collect.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,18 +186,17 @@ impl<'a, 'tcx> AstConv<'tcx, 'tcx> for ItemCtxt<'a, 'tcx> {
186186

187187
fn re_infer(
188188
&self,
189-
_span: Span,
190-
_def: Option<&ty::GenericParamDef>,
189+
_: Option<&ty::GenericParamDef>,
190+
_: Span,
191191
) -> Option<ty::Region<'tcx>> {
192192
None
193193
}
194194

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

0 commit comments

Comments
 (0)