Skip to content

Commit fe02814

Browse files
committed
rustc: Implement floating point literal inference. r=nmatsakis
1 parent f05e2da commit fe02814

20 files changed

+363
-46
lines changed

src/librustc/metadata/tyencode.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,11 @@ fn enc_sty(w: io::Writer, cx: @ctxt, st: ty::sty) {
286286
w.write_char('I');
287287
w.write_uint(id.to_uint());
288288
}
289+
ty::ty_infer(ty::FloatVar(id)) => {
290+
w.write_char('X');
291+
w.write_char('F');
292+
w.write_uint(id.to_uint());
293+
}
289294
ty::ty_param({idx: id, def_id: did}) => {
290295
w.write_char('p');
291296
w.write_str(cx.ds(did));

src/librustc/middle/const_eval.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,8 @@ fn lit_to_const(lit: @lit) -> const_val {
389389
lit_uint(n, _) => const_uint(n),
390390
lit_int_unsuffixed(n) => const_int(n),
391391
lit_float(n, _) => const_float(float::from_str(*n).get() as f64),
392+
lit_float_unsuffixed(n) =>
393+
const_float(float::from_str(*n).get() as f64),
392394
lit_nil => const_int(0i64),
393395
lit_bool(b) => const_bool(b)
394396
}

src/librustc/middle/trans/consts.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ fn const_lit(cx: @crate_ctxt, e: @ast::expr, lit: ast::lit)
2222
}
2323
}
2424
ast::lit_float(fs, t) => C_floating(*fs, T_float_ty(cx, t)),
25+
ast::lit_float_unsuffixed(fs) => {
26+
let lit_float_ty = ty::node_id_to_type(cx.tcx, e.id);
27+
match ty::get(lit_float_ty).sty {
28+
ty::ty_float(t) => {
29+
C_floating(*fs, T_float_ty(cx, t))
30+
}
31+
_ => {
32+
cx.sess.span_bug(lit.span,
33+
~"floating point literal doesn't have the right \
34+
type");
35+
}
36+
}
37+
}
2538
ast::lit_bool(b) => C_bool(b),
2639
ast::lit_nil => C_nil(),
2740
ast::lit_str(s) => C_estr_slice(cx, *s)

src/librustc/middle/ty.rs

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use util::ppaux::{ty_to_str, proto_ty_to_str, tys_to_str};
2121

2222
export ProvidedMethodSource;
2323
export InstantiatedTraitRef;
24-
export TyVid, IntVid, FnVid, RegionVid, vid;
24+
export TyVid, IntVid, FloatVid, FnVid, RegionVid, vid;
2525
export br_hashmap;
2626
export is_instantiable;
2727
export node_id_to_type;
@@ -86,6 +86,7 @@ export ty_fn, FnTy, FnTyBase, FnMeta, FnSig, mk_fn;
8686
export ty_fn_proto, ty_fn_purity, ty_fn_ret, ty_fn_ret_style, tys_in_fn_ty;
8787
export ty_int, mk_int, mk_mach_int, mk_char;
8888
export mk_i8, mk_u8, mk_i16, mk_u16, mk_i32, mk_u32, mk_i64, mk_u64;
89+
export mk_f32, mk_f64;
8990
export ty_estr, mk_estr, type_is_str;
9091
export ty_evec, mk_evec, type_is_vec;
9192
export ty_unboxed_vec, mk_unboxed_vec, mk_mut_unboxed_vec;
@@ -102,8 +103,8 @@ export ty_tup, mk_tup;
102103
export ty_type, mk_type;
103104
export ty_uint, mk_uint, mk_mach_uint;
104105
export ty_uniq, mk_uniq, mk_imm_uniq, type_is_unique_box;
105-
export ty_infer, mk_infer, type_is_ty_var, mk_var, mk_int_var;
106-
export InferTy, TyVar, IntVar;
106+
export ty_infer, mk_infer, type_is_ty_var, mk_var, mk_int_var, mk_float_var;
107+
export InferTy, TyVar, IntVar, FloatVar;
107108
export ty_self, mk_self, type_has_self;
108109
export ty_class;
109110
export Region, bound_region, encl_region;
@@ -172,7 +173,8 @@ export ty_sort_str;
172173
export normalize_ty;
173174
export to_str;
174175
export bound_const;
175-
export terr_no_integral_type, terr_ty_param_size, terr_self_substs;
176+
export terr_no_integral_type, terr_no_floating_point_type;
177+
export terr_ty_param_size, terr_self_substs;
176178
export terr_in_field, terr_record_fields, terr_vstores_differ, terr_arg_count;
177179
export terr_sorts, terr_vec, terr_str, terr_record_size, terr_tuple_size;
178180
export terr_regions_does_not_outlive, terr_mutability, terr_purity_mismatch;
@@ -666,6 +668,7 @@ enum type_err {
666668
terr_sorts(expected_found<t>),
667669
terr_self_substs,
668670
terr_no_integral_type,
671+
terr_no_floating_point_type,
669672
}
670673

671674
enum param_bound {
@@ -678,21 +681,24 @@ enum param_bound {
678681

679682
enum TyVid = uint;
680683
enum IntVid = uint;
684+
enum FloatVid = uint;
681685
enum FnVid = uint;
682686
#[auto_serialize]
683687
#[auto_deserialize]
684688
enum RegionVid = uint;
685689

686690
enum InferTy {
687691
TyVar(TyVid),
688-
IntVar(IntVid)
692+
IntVar(IntVid),
693+
FloatVar(FloatVid)
689694
}
690695

691696
impl InferTy : to_bytes::IterBytes {
692697
pure fn iter_bytes(+lsb0: bool, f: to_bytes::Cb) {
693698
match self {
694699
TyVar(ref tv) => to_bytes::iter_bytes_2(&0u8, tv, lsb0, f),
695-
IntVar(ref iv) => to_bytes::iter_bytes_2(&1u8, iv, lsb0, f)
700+
IntVar(ref iv) => to_bytes::iter_bytes_2(&1u8, iv, lsb0, f),
701+
FloatVar(ref fv) => to_bytes::iter_bytes_2(&2u8, fv, lsb0, f)
696702
}
697703
}
698704
}
@@ -758,6 +764,11 @@ impl IntVid: vid {
758764
pure fn to_str() -> ~str { fmt!("<VI%u>", self.to_uint()) }
759765
}
760766

767+
impl FloatVid: vid {
768+
pure fn to_uint() -> uint { *self }
769+
pure fn to_str() -> ~str { fmt!("<VF%u>", self.to_uint()) }
770+
}
771+
761772
impl FnVid: vid {
762773
pure fn to_uint() -> uint { *self }
763774
pure fn to_str() -> ~str { fmt!("<F%u>", self.to_uint()) }
@@ -773,13 +784,15 @@ impl InferTy {
773784
match self {
774785
TyVar(v) => v.to_uint() << 1,
775786
IntVar(v) => (v.to_uint() << 1) + 1,
787+
FloatVar(v) => (v.to_uint() << 1) + 2
776788
}
777789
}
778790

779791
pure fn to_str() -> ~str {
780792
match self {
781793
TyVar(v) => v.to_str(),
782794
IntVar(v) => v.to_str(),
795+
FloatVar(v) => v.to_str()
783796
}
784797
}
785798
}
@@ -812,6 +825,12 @@ impl IntVid : to_bytes::IterBytes {
812825
}
813826
}
814827

828+
impl FloatVid : to_bytes::IterBytes {
829+
pure fn iter_bytes(+lsb0: bool, f: to_bytes::Cb) {
830+
(*self).iter_bytes(lsb0, f)
831+
}
832+
}
833+
815834
impl FnVid : to_bytes::IterBytes {
816835
pure fn iter_bytes(+lsb0: bool, f: to_bytes::Cb) {
817836
(*self).iter_bytes(lsb0, f)
@@ -1030,6 +1049,10 @@ fn mk_u32(cx: ctxt) -> t { mk_t(cx, ty_uint(ast::ty_u32)) }
10301049

10311050
fn mk_u64(cx: ctxt) -> t { mk_t(cx, ty_uint(ast::ty_u64)) }
10321051

1052+
fn mk_f32(cx: ctxt) -> t { mk_t(cx, ty_float(ast::ty_f32)) }
1053+
1054+
fn mk_f64(cx: ctxt) -> t { mk_t(cx, ty_float(ast::ty_f64)) }
1055+
10331056
fn mk_mach_int(cx: ctxt, tm: ast::int_ty) -> t { mk_t(cx, ty_int(tm)) }
10341057

10351058
fn mk_mach_uint(cx: ctxt, tm: ast::uint_ty) -> t { mk_t(cx, ty_uint(tm)) }
@@ -1110,9 +1133,9 @@ fn mk_class(cx: ctxt, class_id: ast::def_id, +substs: substs) -> t {
11101133

11111134
fn mk_var(cx: ctxt, v: TyVid) -> t { mk_infer(cx, TyVar(v)) }
11121135

1113-
fn mk_int_var(cx: ctxt, v: IntVid) -> t {
1114-
mk_infer(cx, IntVar(v))
1115-
}
1136+
fn mk_int_var(cx: ctxt, v: IntVid) -> t { mk_infer(cx, IntVar(v)) }
1137+
1138+
fn mk_float_var(cx: ctxt, v: FloatVid) -> t { mk_infer(cx, FloatVar(v)) }
11161139

11171140
fn mk_infer(cx: ctxt, it: InferTy) -> t { mk_t(cx, ty_infer(it)) }
11181141

@@ -1661,7 +1684,8 @@ pure fn type_is_unique(ty: t) -> bool {
16611684
pure fn type_is_scalar(ty: t) -> bool {
16621685
match get(ty).sty {
16631686
ty_nil | ty_bool | ty_int(_) | ty_float(_) | ty_uint(_) |
1664-
ty_infer(IntVar(_)) | ty_type | ty_ptr(_) => true,
1687+
ty_infer(IntVar(_)) | ty_infer(FloatVar(_)) | ty_type |
1688+
ty_ptr(_) => true,
16651689
_ => false
16661690
}
16671691
}
@@ -2428,7 +2452,7 @@ fn type_is_integral(ty: t) -> bool {
24282452
24292453
fn type_is_fp(ty: t) -> bool {
24302454
match get(ty).sty {
2431-
ty_float(_) => true,
2455+
ty_infer(FloatVar(_)) | ty_float(_) => true,
24322456
_ => false
24332457
}
24342458
}
@@ -3260,6 +3284,7 @@ fn ty_sort_str(cx: ctxt, t: t) -> ~str {
32603284
ty_tup(_) => ~"tuple",
32613285
ty_infer(TyVar(_)) => ~"inferred type",
32623286
ty_infer(IntVar(_)) => ~"integral variable",
3287+
ty_infer(FloatVar(_)) => ~"floating-point variable",
32633288
ty_param(_) => ~"type parameter",
32643289
ty_self => ~"self"
32653290
}
@@ -3387,6 +3412,10 @@ fn type_err_to_str(cx: ctxt, err: &type_err) -> ~str {
33873412
~"couldn't determine an appropriate integral type for integer \
33883413
literal"
33893414
}
3415+
terr_no_floating_point_type => {
3416+
~"couldn't determine an appropriate floating point type for \
3417+
floating point literal"
3418+
}
33903419
}
33913420
}
33923421

@@ -4000,7 +4029,7 @@ fn is_binopable(_cx: ctxt, ty: t, op: ast::binop) -> bool {
40004029
match get(ty).sty {
40014030
ty_bool => tycat_bool,
40024031
ty_int(_) | ty_uint(_) | ty_infer(IntVar(_)) => tycat_int,
4003-
ty_float(_) => tycat_float,
4032+
ty_float(_) | ty_infer(FloatVar(_)) => tycat_float,
40044033
ty_rec(_) | ty_tup(_) | ty_enum(_, _) => tycat_struct,
40054034
ty_bot => tycat_bot,
40064035
_ => tycat_other
@@ -4230,6 +4259,11 @@ impl IntVid : cmp::Eq {
42304259
pure fn ne(other: &IntVid) -> bool { *self != *(*other) }
42314260
}
42324261

4262+
impl FloatVid : cmp::Eq {
4263+
pure fn eq(other: &FloatVid) -> bool { *self == *(*other) }
4264+
pure fn ne(other: &FloatVid) -> bool { *self != *(*other) }
4265+
}
4266+
42334267
impl FnVid : cmp::Eq {
42344268
pure fn eq(other: &FnVid) -> bool { *self == *(*other) }
42354269
pure fn ne(other: &FnVid) -> bool { *self != *(*other) }

src/librustc/middle/typeck/check.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,11 @@ fn check_lit(fcx: @fn_ctxt, lit: @ast::lit) -> ty::t {
850850
ty::mk_int_var(tcx, fcx.infcx().next_int_var_id())
851851
}
852852
ast::lit_float(_, t) => ty::mk_mach_float(tcx, t),
853+
ast::lit_float_unsuffixed(_) => {
854+
// An unsuffixed floating point literal could have any floating point
855+
// type, so we create a floating point type variable for it.
856+
ty::mk_float_var(tcx, fcx.infcx().next_float_var_id())
857+
}
853858
ast::lit_nil => ty::mk_nil(tcx),
854859
ast::lit_bool(_) => ty::mk_bool(tcx)
855860
}

src/librustc/middle/typeck/check/method.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,7 @@ impl LookupContext {
664664
match ty::get(self_ty).sty {
665665
ty_box(*) | ty_uniq(*) | ty_rptr(*) |
666666
ty_infer(IntVar(_)) | // FIXME(#3211)---should be resolved
667+
ty_infer(FloatVar(_)) | // FIXME(#3211)---should be resolved
667668
ty_self | ty_param(*) | ty_nil | ty_bot | ty_bool |
668669
ty_int(*) | ty_uint(*) |
669670
ty_float(*) | ty_enum(*) | ty_ptr(*) | ty_rec(*) |

src/librustc/middle/typeck/infer.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ overconstrains the type, it's a type error; if we reach the point at
209209
which type variables must be resolved and an integral type variable is
210210
still underconstrained, it defaults to `int` as a last resort.
211211
212+
Floating point types are handled similarly to integral types.
213+
212214
## GLB/LUB
213215
214216
Computing the greatest-lower-bound and least-upper-bound of two
@@ -250,8 +252,8 @@ use std::smallintmap;
250252
use std::smallintmap::smallintmap;
251253
use std::map::HashMap;
252254
use middle::ty;
253-
use middle::ty::{TyVid, IntVid, RegionVid, vid,
254-
ty_int, ty_uint, get, terr_fn, TyVar, IntVar};
255+
use middle::ty::{TyVid, IntVid, FloatVid, RegionVid, vid,
256+
ty_int, ty_uint, get, terr_fn, TyVar, IntVar, FloatVar};
255257
use syntax::{ast, ast_util};
256258
use syntax::ast::{ret_style, purity};
257259
use util::ppaux::{ty_to_str, mt_to_str};
@@ -272,6 +274,7 @@ use resolve::{resolve_nested_tvar, resolve_rvar, resolve_ivar, resolve_all,
272274
resolve_and_force_all_but_regions, resolver};
273275
use unify::{vals_and_bindings, root};
274276
use integral::{int_ty_set, int_ty_set_all};
277+
use floating::{float_ty_set, float_ty_set_all};
275278
use combine::{combine_fields, eq_tys};
276279
use assignment::Assign;
277280
use to_str::ToStr;
@@ -318,12 +321,17 @@ enum infer_ctxt = @{
318321
// represented by an int_ty_set.
319322
int_var_bindings: vals_and_bindings<ty::IntVid, int_ty_set>,
320323

324+
// The types that might instantiate a floating-point type variable are
325+
// represented by an float_ty_set.
326+
float_var_bindings: vals_and_bindings<ty::FloatVid, float_ty_set>,
327+
321328
// For region variables.
322329
region_vars: RegionVarBindings,
323330

324331
// For keeping track of existing type and region variables.
325332
ty_var_counter: @mut uint,
326333
int_var_counter: @mut uint,
334+
float_var_counter: @mut uint,
327335
region_var_counter: @mut uint
328336
};
329337

@@ -359,9 +367,11 @@ fn new_infer_ctxt(tcx: ty::ctxt) -> infer_ctxt {
359367
infer_ctxt(@{tcx: tcx,
360368
ty_var_bindings: new_vals_and_bindings(),
361369
int_var_bindings: new_vals_and_bindings(),
370+
float_var_bindings: new_vals_and_bindings(),
362371
region_vars: RegionVarBindings(tcx),
363372
ty_var_counter: @mut 0u,
364373
int_var_counter: @mut 0u,
374+
float_var_counter: @mut 0u,
365375
region_var_counter: @mut 0u})}
366376

367377
fn mk_subty(cx: infer_ctxt, a_is_expected: bool, span: span,
@@ -627,6 +637,18 @@ impl infer_ctxt {
627637
ty::mk_int_var(self.tcx, self.next_int_var_id())
628638
}
629639

640+
fn next_float_var_id() -> FloatVid {
641+
let id = *self.float_var_counter;
642+
*self.float_var_counter += 1;
643+
644+
self.float_var_bindings.vals.insert(id, root(float_ty_set_all(), 0));
645+
return FloatVid(id);
646+
}
647+
648+
fn next_float_var() -> ty::t {
649+
ty::mk_float_var(self.tcx, self.next_float_var_id())
650+
}
651+
630652
fn next_region_var_nb(span: span) -> ty::Region {
631653
ty::re_infer(ty::ReVar(self.region_vars.new_region_var(span)))
632654
}

src/librustc/middle/typeck/infer/combine.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,17 @@ fn super_tys<C:combine>(
385385
self.infcx().t_sub_int_var(a, b_id).then(|| Ok(a) )
386386
}
387387

388+
// Relate floating-point variables to other types
389+
(ty::ty_infer(FloatVar(a_id)), ty::ty_infer(FloatVar(b_id))) => {
390+
self.infcx().float_vars(a_id, b_id).then(|| Ok(a) )
391+
}
392+
(ty::ty_infer(FloatVar(a_id)), ty::ty_float(_)) => {
393+
self.infcx().float_var_sub_t(a_id, b).then(|| Ok(a) )
394+
}
395+
(ty::ty_float(_), ty::ty_infer(FloatVar(b_id))) => {
396+
self.infcx().t_sub_float_var(a, b_id).then(|| Ok(a) )
397+
}
398+
388399
(ty::ty_int(_), _) |
389400
(ty::ty_uint(_), _) |
390401
(ty::ty_float(_), _) => {

0 commit comments

Comments
 (0)