Skip to content

Commit 8eb12d9

Browse files
committed
remove rustc_typeck::same_type_err
1 parent b2422ab commit 8eb12d9

File tree

12 files changed

+114
-55
lines changed

12 files changed

+114
-55
lines changed

src/librustc/infer/error_reporting.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
554554
trace: TypeTrace<'tcx>,
555555
terr: &TypeError<'tcx>)
556556
-> DiagnosticBuilder<'tcx> {
557+
let trace = self.resolve_type_vars_if_possible(&trace);
557558
let span = trace.origin.span();
558559
let mut err = self.report_type_error(trace, terr);
559560
self.tcx.note_and_explain_type_err(&mut err, terr, span);
@@ -1643,6 +1644,15 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
16431644
TypeOrigin::EquatePredicate(_) => {
16441645
"equality where clause is satisfied"
16451646
}
1647+
TypeOrigin::MainFunctionType(_) => {
1648+
"the `main` function has the correct type"
1649+
}
1650+
TypeOrigin::StartFunctionType(_) => {
1651+
"the `start` function has the correct type"
1652+
}
1653+
TypeOrigin::IntrinsicType(_) => {
1654+
"the intrinsic has the correct type"
1655+
}
16461656
};
16471657

16481658
match self.values_str(&trace.values) {

src/librustc/infer/mod.rs

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use ty::adjustment;
3232
use ty::{TyVid, IntVid, FloatVid};
3333
use ty::{self, Ty, TyCtxt};
3434
use ty::error::{ExpectedFound, TypeError, UnconstrainedNumeric};
35-
use ty::fold::TypeFoldable;
35+
use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
3636
use ty::relate::{Relate, RelateResult, TypeRelation};
3737
use traits::{self, PredicateObligations, ProjectionMode};
3838
use rustc_data_structures::unify::{self, UnificationTable};
@@ -219,6 +219,15 @@ pub enum TypeOrigin {
219219

220220
// `where a == b`
221221
EquatePredicate(Span),
222+
223+
// `main` has wrong type
224+
MainFunctionType(Span),
225+
226+
// `start` has wrong type
227+
StartFunctionType(Span),
228+
229+
// intrinsic has wrong type
230+
IntrinsicType(Span),
222231
}
223232

224233
impl TypeOrigin {
@@ -238,6 +247,9 @@ impl TypeOrigin {
238247
&TypeOrigin::IfExpressionWithNoElse(_) => "if may be missing an else clause",
239248
&TypeOrigin::RangeExpression(_) => "start and end of range have incompatible types",
240249
&TypeOrigin::EquatePredicate(_) => "equality predicate not satisfied",
250+
&TypeOrigin::MainFunctionType(_) => "main function has wrong type",
251+
&TypeOrigin::StartFunctionType(_) => "start function has wrong type",
252+
&TypeOrigin::IntrinsicType(_) => "intrinsic has wrong type",
241253
}
242254
}
243255
}
@@ -1791,6 +1803,9 @@ impl TypeOrigin {
17911803
TypeOrigin::IfExpressionWithNoElse(span) => span,
17921804
TypeOrigin::RangeExpression(span) => span,
17931805
TypeOrigin::EquatePredicate(span) => span,
1806+
TypeOrigin::MainFunctionType(span) => span,
1807+
TypeOrigin::StartFunctionType(span) => span,
1808+
TypeOrigin::IntrinsicType(span) => span,
17941809
}
17951810
}
17961811
}
@@ -1841,3 +1856,50 @@ impl RegionVariableOrigin {
18411856
}
18421857
}
18431858
}
1859+
1860+
impl<'tcx> TypeFoldable<'tcx> for TypeOrigin {
1861+
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, _folder: &mut F) -> Self {
1862+
self.clone()
1863+
}
1864+
1865+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
1866+
false
1867+
}
1868+
}
1869+
1870+
impl<'tcx> TypeFoldable<'tcx> for ValuePairs<'tcx> {
1871+
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
1872+
match *self {
1873+
ValuePairs::Types(ref ef) => {
1874+
ValuePairs::Types(ef.fold_with(folder))
1875+
}
1876+
ValuePairs::TraitRefs(ref ef) => {
1877+
ValuePairs::TraitRefs(ef.fold_with(folder))
1878+
}
1879+
ValuePairs::PolyTraitRefs(ref ef) => {
1880+
ValuePairs::PolyTraitRefs(ef.fold_with(folder))
1881+
}
1882+
}
1883+
}
1884+
1885+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1886+
match *self {
1887+
ValuePairs::Types(ref ef) => ef.visit_with(visitor),
1888+
ValuePairs::TraitRefs(ref ef) => ef.visit_with(visitor),
1889+
ValuePairs::PolyTraitRefs(ref ef) => ef.visit_with(visitor),
1890+
}
1891+
}
1892+
}
1893+
1894+
impl<'tcx> TypeFoldable<'tcx> for TypeTrace<'tcx> {
1895+
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
1896+
TypeTrace {
1897+
origin: self.origin.fold_with(folder),
1898+
values: self.values.fold_with(folder)
1899+
}
1900+
}
1901+
1902+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1903+
self.origin.visit_with(visitor) || self.values.visit_with(visitor)
1904+
}
1905+
}

src/librustc/ty/structural_impls.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,3 +1018,16 @@ impl<'tcx> TypeFoldable<'tcx> for ty::TypeScheme<'tcx> {
10181018
self.generics.visit_with(visitor) || self.ty.visit_with(visitor)
10191019
}
10201020
}
1021+
1022+
impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::error::ExpectedFound<T> {
1023+
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
1024+
ty::error::ExpectedFound {
1025+
expected: self.expected.fold_with(folder),
1026+
found: self.found.fold_with(folder),
1027+
}
1028+
}
1029+
1030+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1031+
self.expected.visit_with(visitor) || self.found.visit_with(visitor)
1032+
}
1033+
}

src/librustc_typeck/check/_match.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
103103
return;
104104
}
105105

106-
// Check that the types of the end-points can be unified.
107-
let types_unify = self.require_same_types(pat.span, rhs_ty, lhs_ty,
108-
"mismatched types in range");
109-
110-
// It's ok to return without a message as `require_same_types` prints an error.
111-
if !types_unify {
112-
return;
113-
}
114-
115106
// Now that we know the types can be unified we find the unified type and use
116107
// it to type the entire expression.
117108
let common_type = self.resolve_type_vars_if_possible(&lhs_ty);
@@ -120,6 +111,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
120111

121112
// subtyping doesn't matter here, as the value is some kind of scalar
122113
self.demand_eqtype(pat.span, expected, lhs_ty);
114+
self.demand_eqtype(pat.span, expected, rhs_ty);
123115
}
124116
PatKind::Binding(bm, _, ref sub) => {
125117
let typ = self.local_ty(pat.span, pat.id);

src/librustc_typeck/check/demand.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,4 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
5454
self.report_mismatched_types(origin, expected, expr_ty, e);
5555
}
5656
}
57-
58-
pub fn require_same_types(&self, span: Span, t1: Ty<'tcx>, t2: Ty<'tcx>, msg: &str)
59-
-> bool {
60-
if let Err(err) = self.eq_types(false, TypeOrigin::Misc(span), t1, t2) {
61-
let found_ty = self.resolve_type_vars_if_possible(&t1);
62-
let expected_ty = self.resolve_type_vars_if_possible(&t2);
63-
::emit_type_err(self.tcx, span, found_ty, expected_ty, &err, msg);
64-
false
65-
} else {
66-
true
67-
}
68-
}
6957
}

src/librustc_typeck/check/intrinsic.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//! intrinsics that the compiler exposes.
1313
1414
use intrinsics;
15+
use rustc::infer::TypeOrigin;
1516
use rustc::ty::subst::{self, Substs};
1617
use rustc::ty::FnSig;
1718
use rustc::ty::{self, Ty};
@@ -56,10 +57,9 @@ fn equate_intrinsic_type<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
5657
i_n_tps, n_tps);
5758
} else {
5859
require_same_types(ccx,
59-
it.span,
60+
TypeOrigin::IntrinsicType(it.span),
6061
i_ty.ty,
61-
fty,
62-
"intrinsic has wrong type");
62+
fty);
6363
}
6464
}
6565

src/librustc_typeck/check/wfcheck.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,7 @@ impl<'ccx, 'gcx> CheckTypeWellFormedVisitor<'ccx, 'gcx> {
437437

438438
debug!("check_method_receiver: receiver ty = {:?}", rcvr_ty);
439439

440-
fcx.require_same_types(span, sig.inputs[0], rcvr_ty,
441-
"mismatched method receiver");
440+
fcx.demand_eqtype(span, rcvr_ty, sig.inputs[0]);
442441
}
443442

444443
fn check_variances_for_type_defn(&self,

src/librustc_typeck/diagnostics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2660,6 +2660,7 @@ For information on the design of the orphan rules, see [RFC 1023].
26602660
[RFC 1023]: https://github.com/rust-lang/rfcs/pull/1023
26612661
"##,
26622662

2663+
/*
26632664
E0211: r##"
26642665
You used a function or type which doesn't fit the requirements for where it was
26652666
used. Erroneous code examples:
@@ -2739,6 +2740,7 @@ impl Foo {
27392740
}
27402741
```
27412742
"##,
2743+
*/
27422744

27432745
E0214: r##"
27442746
A generic type was described using parentheses rather than angle brackets. For

src/librustc_typeck/lib.rs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -186,28 +186,14 @@ fn require_c_abi_if_variadic(tcx: TyCtxt,
186186
}
187187
}
188188

189-
pub fn emit_type_err<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
190-
span: Span,
191-
found_ty: Ty<'tcx>,
192-
expected_ty: Ty<'tcx>,
193-
terr: &ty::error::TypeError<'tcx>,
194-
msg: &str) {
195-
let mut err = struct_span_err!(tcx.sess, span, E0211, "{}", msg);
196-
err.span_label(span, &terr);
197-
err.note_expected_found(&"type", &expected_ty, &found_ty);
198-
tcx.note_and_explain_type_err(&mut err, terr, span);
199-
err.emit();
200-
}
201-
202189
fn require_same_types<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
203-
span: Span,
190+
origin: TypeOrigin,
204191
t1: Ty<'tcx>,
205-
t2: Ty<'tcx>,
206-
msg: &str)
192+
t2: Ty<'tcx>)
207193
-> bool {
208194
ccx.tcx.infer_ctxt(None, None, ProjectionMode::AnyFinal).enter(|infcx| {
209-
if let Err(err) = infcx.eq_types(false, TypeOrigin::Misc(span), t1, t2) {
210-
emit_type_err(infcx.tcx, span, t1, t2, &err, msg);
195+
if let Err(err) = infcx.eq_types(false, origin.clone(), t1, t2) {
196+
infcx.report_mismatched_types(origin, t1, t2, err);
211197
false
212198
} else {
213199
true
@@ -249,8 +235,11 @@ fn check_main_fn_ty(ccx: &CrateCtxt,
249235
})
250236
}));
251237

252-
require_same_types(ccx, main_span, main_t, se_ty,
253-
"main function has wrong type");
238+
require_same_types(
239+
ccx,
240+
TypeOrigin::MainFunctionType(main_span),
241+
main_t,
242+
se_ty);
254243
}
255244
_ => {
256245
span_bug!(main_span,
@@ -298,8 +287,11 @@ fn check_start_fn_ty(ccx: &CrateCtxt,
298287
}),
299288
}));
300289

301-
require_same_types(ccx, start_span, start_t, se_ty,
302-
"start function has wrong type");
290+
require_same_types(
291+
ccx,
292+
TypeOrigin::StartFunctionType(start_span),
293+
start_t,
294+
se_ty);
303295
}
304296
_ => {
305297
span_bug!(start_span,

src/test/compile-fail/issue-26194.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct S(String);
1212

1313
impl S {
1414
fn f(self: *mut S) -> String { self.0 }
15-
//~^ ERROR mismatched method receiver
15+
//~^ ERROR mismatched types
1616
}
1717

1818
fn main() { S("".to_owned()).f(); }

src/test/compile-fail/match-range-fail.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ fn main() {
2727
'c' ... 100 => { }
2828
_ => { }
2929
};
30-
//~^^^ ERROR mismatched types in range
31-
//~| expected char, found integral variable
30+
//~^^^ ERROR mismatched types
31+
//~| expected type `_`
32+
//~| found type `char`
3233
}

src/test/compile-fail/ufcs-explicit-self-bad.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct Foo {
1515
}
1616

1717
impl Foo {
18-
fn foo(self: isize, x: isize) -> isize { //~ ERROR mismatched method receiver
18+
fn foo(self: isize, x: isize) -> isize { //~ ERROR mismatched types
1919
self.f + x
2020
}
2121
}
@@ -25,10 +25,10 @@ struct Bar<T> {
2525
}
2626

2727
impl<T> Bar<T> {
28-
fn foo(self: Bar<isize>, x: isize) -> isize { //~ ERROR mismatched method receiver
28+
fn foo(self: Bar<isize>, x: isize) -> isize { //~ ERROR mismatched types
2929
x
3030
}
31-
fn bar(self: &Bar<usize>, x: isize) -> isize { //~ ERROR mismatched method receiver
31+
fn bar(self: &Bar<usize>, x: isize) -> isize { //~ ERROR mismatched types
3232
x
3333
}
3434
}

0 commit comments

Comments
 (0)