Skip to content

Commit 9da04b2

Browse files
jroeschJared Roesch
authored and
Jared Roesch
committed
Make default error reporting deterministic
1 parent d732f73 commit 9da04b2

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,6 +1800,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18001800
// We wrap this in a transaction for error reporting, if we detect a conflict
18011801
// we will rollback the inference context to its prior state so we can probe
18021802
// for conflicts and correctly report them.
1803+
1804+
18031805
let _ = self.infcx().commit_if_ok(|_: &infer::CombinedSnapshot| {
18041806
for ty in &unbound_tyvars {
18051807
if self.infcx().type_var_diverges(ty) {
@@ -1849,10 +1851,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18491851
def_id: local_def(0) // what do I put here?
18501852
});
18511853

1854+
// This is to ensure that we elimnate any non-determinism from the error
1855+
// reporting by fixing an order, it doesn't matter what order we choose
1856+
// just that it is consistent.
1857+
let (first_default, second_default) =
1858+
if default.def_id < conflicting_default.def_id {
1859+
(default, conflicting_default)
1860+
} else {
1861+
(conflicting_default, default)
1862+
};
1863+
1864+
18521865
self.infcx().report_conflicting_default_types(
1853-
conflicting_default.origin_span,
1854-
conflicting_default,
1855-
default)
1866+
first_default.origin_span,
1867+
first_default,
1868+
second_default)
18561869
}
18571870
}
18581871
}

src/librustc_typeck/collect.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1930,7 +1930,6 @@ fn get_or_create_type_parameter_def<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
19301930
index: index,
19311931
name: param.ident.name,
19321932
def_id: local_def(param.id),
1933-
// what do I return? should this be an option as well
19341933
default_def_id: local_def(parent),
19351934
default: default,
19361935
object_lifetime_default: object_lifetime_default,

src/test/compile-fail/default_ty_param_conflict.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,19 @@ use std::fmt::Debug;
1212

1313
// Example from the RFC
1414
fn foo<F:Default=usize>() -> F { F::default() }
15+
//~^ NOTE: a default was defined here...
16+
1517
fn bar<B:Debug=isize>(b: B) { println!("{:?}", b); }
18+
//~^ NOTE: a second default was defined here...
1619

1720
fn main() {
1821
// Here, F is instantiated with $0=uint
1922
let x = foo();
23+
//~^ ERROR: mismatched types
24+
//~| NOTE: conflicting type parameter defaults `usize` and `isize`
25+
//~| NOTE: ...that was applied to an unconstrained type variable here
2026

2127
// Here, B is instantiated with $1=uint, and constraint $0 <: $1 is added.
2228
bar(x);
29+
//~^ NOTE: ...that also applies to the same type variable here
2330
}

src/test/compile-fail/default_ty_param_conflict_cross_crate.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@
77
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
10+
//
11+
//aux-build:default_ty_param_cross_crate_crate.rs
12+
extern crate default_param_test;
1013

11-
use std::fmt::Debug;
12-
use std::collections::HashMap;
14+
use default_param_test::{Foo, bleh};
1315

14-
fn foo<R=()>(x: HashMap<i32, i32, R>) -> HashMap<i32, i32, R> { x }
15-
fn bar<R=char>(x: HashMap<i32, i32, R>) {}
16+
fn meh<X, B=bool>(x: Foo<X, B>) {}
17+
//~^ NOTE: a default was defined here...
1618

1719
fn main() {
18-
let x: HashMap<i32, i32, _> = foo(panic!());
19-
bar(x);
20+
let foo = bleh();
21+
//~^ NOTE: ...that also applies to the same type variable here
22+
23+
meh(foo);
24+
//~^ ERROR: mismatched types:
25+
//~| NOTE: conflicting type parameter defaults `bool` and `char`
2026
}

0 commit comments

Comments
 (0)