2014-02-10 08:36:31 -06:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2011-06-27 16:37:02 -05:00
|
|
|
// Type encoding
|
|
|
|
|
2014-03-21 20:05:05 -05:00
|
|
|
#![allow(unused_must_use)] // as with encoding, everything is a no-fail MemWriter
|
|
|
|
#![allow(non_camel_case_types)]
|
2014-01-29 20:42:19 -06:00
|
|
|
|
2013-12-20 22:18:52 -06:00
|
|
|
use std::cell::RefCell;
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2014-05-13 10:35:42 -05:00
|
|
|
use middle::subst;
|
2014-05-31 17:53:13 -05:00
|
|
|
use middle::subst::VecPerParamSpace;
|
|
|
|
use middle::ty::ParamTy;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::ty;
|
2014-11-09 16:59:56 -06:00
|
|
|
use util::nodemap::FnvHashMap;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2014-04-02 03:19:41 -05:00
|
|
|
use syntax::abi::Abi;
|
2013-04-02 02:40:57 -05:00
|
|
|
use syntax::ast;
|
2012-09-04 13:54:36 -05:00
|
|
|
use syntax::ast::*;
|
2013-12-31 08:17:59 -06:00
|
|
|
use syntax::diagnostic::SpanHandler;
|
2014-02-13 23:07:09 -06:00
|
|
|
use syntax::parse::token;
|
2011-06-27 16:37:02 -05:00
|
|
|
|
2014-07-29 20:27:28 -05:00
|
|
|
use rbml::io::SeekableMemWriter;
|
2014-07-29 18:31:39 -05:00
|
|
|
|
2014-05-10 16:05:06 -05:00
|
|
|
macro_rules! mywrite( ($($arg:tt)*) => ({ write!($($arg)*); }) )
|
2013-10-21 19:11:42 -05:00
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
pub struct ctxt<'a, 'tcx: 'a> {
|
2014-03-28 12:05:27 -05:00
|
|
|
pub diag: &'a SpanHandler,
|
2011-09-02 17:34:58 -05:00
|
|
|
// Def -> str Callback:
|
2014-05-22 18:57:53 -05:00
|
|
|
pub ds: fn(DefId) -> String,
|
2011-09-02 17:34:58 -05:00
|
|
|
// The type context.
|
2014-04-22 07:56:37 -05:00
|
|
|
pub tcx: &'a ty::ctxt<'tcx>,
|
2014-04-22 11:06:43 -05:00
|
|
|
pub abbrevs: &'a abbrev_map
|
2013-01-08 16:00:45 -06:00
|
|
|
}
|
2011-06-27 16:37:02 -05:00
|
|
|
|
|
|
|
// Compact string representation for ty.t values. API ty_str & parse_from_str.
|
|
|
|
// Extra parameters are for converting to/from def_ids in the string rep.
|
|
|
|
// Whatever format you choose should not contain pipe characters.
|
2013-02-19 01:40:42 -06:00
|
|
|
pub struct ty_abbrev {
|
2014-05-22 18:57:53 -05:00
|
|
|
s: String
|
2013-02-19 01:40:42 -06:00
|
|
|
}
|
2011-06-27 16:37:02 -05:00
|
|
|
|
2014-11-09 16:59:56 -06:00
|
|
|
pub type abbrev_map = RefCell<FnvHashMap<ty::t, ty_abbrev>>;
|
2011-06-27 16:37:02 -05:00
|
|
|
|
2014-07-29 18:31:39 -05:00
|
|
|
pub fn enc_ty(w: &mut SeekableMemWriter, cx: &ctxt, t: ty::t) {
|
2014-11-06 11:25:16 -06:00
|
|
|
match cx.abbrevs.borrow_mut().get(&t) {
|
2014-05-28 11:24:28 -05:00
|
|
|
Some(a) => { w.write(a.s.as_bytes()); return; }
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
let pos = w.tell().unwrap();
|
|
|
|
enc_sty(w, cx, &ty::get(t).sty);
|
|
|
|
let end = w.tell().unwrap();
|
|
|
|
let len = end - pos;
|
|
|
|
fn estimate_sz(u: u64) -> u64 {
|
|
|
|
let mut n = u;
|
|
|
|
let mut len = 0;
|
|
|
|
while n != 0 { len += 1; n = n >> 4; }
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
let abbrev_len = 3 + estimate_sz(pos) + estimate_sz(len);
|
|
|
|
if abbrev_len < len {
|
|
|
|
// I.e. it's actually an abbreviation.
|
|
|
|
cx.abbrevs.borrow_mut().insert(t, ty_abbrev {
|
|
|
|
s: format!("#{:x}:{:x}#", pos, len)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2013-04-02 02:40:57 -05:00
|
|
|
|
2014-07-29 18:31:39 -05:00
|
|
|
fn enc_mutability(w: &mut SeekableMemWriter, mt: ast::Mutability) {
|
2013-04-02 02:40:57 -05:00
|
|
|
match mt {
|
2013-10-21 19:11:42 -05:00
|
|
|
MutImmutable => (),
|
|
|
|
MutMutable => mywrite!(w, "m"),
|
2011-06-27 16:37:02 -05:00
|
|
|
}
|
2013-04-02 02:40:57 -05:00
|
|
|
}
|
|
|
|
|
2014-07-29 18:31:39 -05:00
|
|
|
fn enc_mt(w: &mut SeekableMemWriter, cx: &ctxt, mt: ty::mt) {
|
2013-04-02 02:40:57 -05:00
|
|
|
enc_mutability(w, mt.mutbl);
|
2011-06-27 16:37:02 -05:00
|
|
|
enc_ty(w, cx, mt.ty);
|
|
|
|
}
|
2012-04-18 23:26:25 -05:00
|
|
|
|
2014-07-29 18:31:39 -05:00
|
|
|
fn enc_opt<T>(w: &mut SeekableMemWriter, t: Option<T>, enc_f: |&mut SeekableMemWriter, T|) {
|
2013-04-17 11:15:37 -05:00
|
|
|
match t {
|
2013-10-21 19:11:42 -05:00
|
|
|
None => mywrite!(w, "n"),
|
|
|
|
Some(v) => {
|
|
|
|
mywrite!(w, "s");
|
2013-12-20 19:36:07 -06:00
|
|
|
enc_f(w, v);
|
2013-10-21 19:11:42 -05:00
|
|
|
}
|
2012-04-01 16:28:30 -05:00
|
|
|
}
|
|
|
|
}
|
2012-04-18 23:26:25 -05:00
|
|
|
|
2014-07-29 18:31:39 -05:00
|
|
|
fn enc_vec_per_param_space<T>(w: &mut SeekableMemWriter,
|
2014-05-31 17:53:13 -05:00
|
|
|
cx: &ctxt,
|
|
|
|
v: &VecPerParamSpace<T>,
|
2014-07-29 18:31:39 -05:00
|
|
|
op: |&mut SeekableMemWriter, &ctxt, &T|) {
|
2014-05-31 17:53:13 -05:00
|
|
|
for &space in subst::ParamSpace::all().iter() {
|
|
|
|
mywrite!(w, "[");
|
2014-07-04 09:39:28 -05:00
|
|
|
for t in v.get_slice(space).iter() {
|
2014-05-31 17:53:13 -05:00
|
|
|
op(w, cx, t);
|
|
|
|
}
|
|
|
|
mywrite!(w, "]");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-29 18:31:39 -05:00
|
|
|
pub fn enc_substs(w: &mut SeekableMemWriter, cx: &ctxt, substs: &subst::Substs) {
|
2013-07-24 15:52:57 -05:00
|
|
|
enc_region_substs(w, cx, &substs.regions);
|
2014-05-31 17:53:13 -05:00
|
|
|
enc_vec_per_param_space(w, cx, &substs.types,
|
|
|
|
|w, cx, &ty| enc_ty(w, cx, ty));
|
2012-04-18 23:26:25 -05:00
|
|
|
}
|
|
|
|
|
2014-07-29 18:31:39 -05:00
|
|
|
fn enc_region_substs(w: &mut SeekableMemWriter, cx: &ctxt, substs: &subst::RegionSubsts) {
|
2013-07-24 15:52:57 -05:00
|
|
|
match *substs {
|
2014-05-13 10:35:42 -05:00
|
|
|
subst::ErasedRegions => {
|
2013-10-21 19:11:42 -05:00
|
|
|
mywrite!(w, "e");
|
2013-07-24 15:52:57 -05:00
|
|
|
}
|
2014-05-13 10:35:42 -05:00
|
|
|
subst::NonerasedRegions(ref regions) => {
|
2013-10-21 19:11:42 -05:00
|
|
|
mywrite!(w, "n");
|
2014-05-31 17:53:13 -05:00
|
|
|
enc_vec_per_param_space(w, cx, regions,
|
|
|
|
|w, cx, &r| enc_region(w, cx, r));
|
2013-07-24 15:52:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-27 20:46:52 -05:00
|
|
|
pub fn enc_region(w: &mut SeekableMemWriter, cx: &ctxt, r: ty::Region) {
|
2012-08-06 14:34:08 -05:00
|
|
|
match r {
|
2013-10-29 09:34:11 -05:00
|
|
|
ty::ReLateBound(id, br) => {
|
2014-11-15 15:47:59 -06:00
|
|
|
mywrite!(w, "b[{}|", id.depth);
|
2013-10-21 19:11:42 -05:00
|
|
|
enc_bound_region(w, cx, br);
|
2013-10-29 05:03:32 -05:00
|
|
|
mywrite!(w, "]");
|
|
|
|
}
|
2014-05-31 17:53:13 -05:00
|
|
|
ty::ReEarlyBound(node_id, space, index, name) => {
|
|
|
|
mywrite!(w, "B[{}|{}|{}|{}]",
|
2013-10-29 05:03:32 -05:00
|
|
|
node_id,
|
2014-05-31 17:53:13 -05:00
|
|
|
space.to_uint(),
|
2013-10-29 05:03:32 -05:00
|
|
|
index,
|
2014-03-04 23:59:35 -06:00
|
|
|
token::get_name(name));
|
2013-10-21 19:11:42 -05:00
|
|
|
}
|
2013-10-29 09:34:11 -05:00
|
|
|
ty::ReFree(ref fr) => {
|
2013-10-21 19:11:42 -05:00
|
|
|
mywrite!(w, "f[{}|", fr.scope_id);
|
|
|
|
enc_bound_region(w, cx, fr.bound_region);
|
|
|
|
mywrite!(w, "]");
|
|
|
|
}
|
2013-10-29 09:34:11 -05:00
|
|
|
ty::ReScope(nid) => {
|
2013-10-29 05:03:32 -05:00
|
|
|
mywrite!(w, "s{}|", nid);
|
|
|
|
}
|
2013-10-29 09:34:11 -05:00
|
|
|
ty::ReStatic => {
|
2013-10-29 05:03:32 -05:00
|
|
|
mywrite!(w, "t");
|
|
|
|
}
|
2013-10-29 09:34:11 -05:00
|
|
|
ty::ReEmpty => {
|
2013-10-29 05:03:32 -05:00
|
|
|
mywrite!(w, "e");
|
|
|
|
}
|
2013-10-29 09:34:11 -05:00
|
|
|
ty::ReInfer(_) => {
|
2013-10-21 19:11:42 -05:00
|
|
|
// these should not crop up after typeck
|
2014-02-06 03:38:08 -06:00
|
|
|
cx.diag.handler().bug("cannot encode region variables");
|
2013-10-21 19:11:42 -05:00
|
|
|
}
|
2012-04-18 23:26:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-29 18:31:39 -05:00
|
|
|
fn enc_bound_region(w: &mut SeekableMemWriter, cx: &ctxt, br: ty::BoundRegion) {
|
2012-08-06 14:34:08 -05:00
|
|
|
match br {
|
2013-10-29 09:34:11 -05:00
|
|
|
ty::BrAnon(idx) => {
|
2013-10-29 05:03:32 -05:00
|
|
|
mywrite!(w, "a{}|", idx);
|
|
|
|
}
|
2014-03-04 23:59:35 -06:00
|
|
|
ty::BrNamed(d, name) => {
|
2013-10-29 05:03:32 -05:00
|
|
|
mywrite!(w, "[{}|{}]",
|
|
|
|
(cx.ds)(d),
|
2014-03-04 23:59:35 -06:00
|
|
|
token::get_name(name));
|
2013-10-29 05:03:32 -05:00
|
|
|
}
|
2013-10-29 09:34:11 -05:00
|
|
|
ty::BrFresh(id) => {
|
2013-10-29 05:03:32 -05:00
|
|
|
mywrite!(w, "f{}|", id);
|
|
|
|
}
|
2014-10-07 22:04:45 -05:00
|
|
|
ty::BrEnv => {
|
|
|
|
mywrite!(w, "e|");
|
|
|
|
}
|
2012-03-08 16:05:16 -06:00
|
|
|
}
|
|
|
|
}
|
2012-04-10 20:32:51 -05:00
|
|
|
|
2014-07-29 18:31:39 -05:00
|
|
|
pub fn enc_trait_ref(w: &mut SeekableMemWriter, cx: &ctxt, s: &ty::TraitRef) {
|
2013-10-21 19:11:42 -05:00
|
|
|
mywrite!(w, "{}|", (cx.ds)(s.def_id));
|
2013-04-17 11:15:37 -05:00
|
|
|
enc_substs(w, cx, &s.substs);
|
2013-03-27 05:16:28 -05:00
|
|
|
}
|
|
|
|
|
2014-07-29 18:31:39 -05:00
|
|
|
pub fn enc_trait_store(w: &mut SeekableMemWriter, cx: &ctxt, s: ty::TraitStore) {
|
2013-03-08 23:16:09 -06:00
|
|
|
match s {
|
2013-10-21 19:11:42 -05:00
|
|
|
ty::UniqTraitStore => mywrite!(w, "~"),
|
2014-04-11 01:01:31 -05:00
|
|
|
ty::RegionTraitStore(re, m) => {
|
2013-10-21 19:11:42 -05:00
|
|
|
mywrite!(w, "&");
|
2013-03-08 23:16:09 -06:00
|
|
|
enc_region(w, cx, re);
|
2014-04-11 01:01:31 -05:00
|
|
|
enc_mutability(w, m);
|
2013-03-08 23:16:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-29 18:31:39 -05:00
|
|
|
fn enc_sty(w: &mut SeekableMemWriter, cx: &ctxt, st: &ty::sty) {
|
2013-07-02 14:47:32 -05:00
|
|
|
match *st {
|
2013-10-21 19:11:42 -05:00
|
|
|
ty::ty_bool => mywrite!(w, "b"),
|
|
|
|
ty::ty_char => mywrite!(w, "c"),
|
|
|
|
ty::ty_int(t) => {
|
|
|
|
match t {
|
2014-01-09 07:05:33 -06:00
|
|
|
TyI => mywrite!(w, "i"),
|
|
|
|
TyI8 => mywrite!(w, "MB"),
|
|
|
|
TyI16 => mywrite!(w, "MW"),
|
|
|
|
TyI32 => mywrite!(w, "ML"),
|
|
|
|
TyI64 => mywrite!(w, "MD")
|
2013-10-21 19:11:42 -05:00
|
|
|
}
|
2011-12-07 14:06:12 -06:00
|
|
|
}
|
2013-10-21 19:11:42 -05:00
|
|
|
ty::ty_uint(t) => {
|
|
|
|
match t {
|
2014-01-09 07:05:33 -06:00
|
|
|
TyU => mywrite!(w, "u"),
|
|
|
|
TyU8 => mywrite!(w, "Mb"),
|
|
|
|
TyU16 => mywrite!(w, "Mw"),
|
|
|
|
TyU32 => mywrite!(w, "Ml"),
|
|
|
|
TyU64 => mywrite!(w, "Md")
|
2013-10-21 19:11:42 -05:00
|
|
|
}
|
2011-12-07 14:06:12 -06:00
|
|
|
}
|
2013-10-21 19:11:42 -05:00
|
|
|
ty::ty_float(t) => {
|
|
|
|
match t {
|
2014-01-09 07:05:33 -06:00
|
|
|
TyF32 => mywrite!(w, "Mf"),
|
|
|
|
TyF64 => mywrite!(w, "MF"),
|
2013-10-21 19:11:42 -05:00
|
|
|
}
|
2011-06-27 16:37:02 -05:00
|
|
|
}
|
2013-10-21 19:11:42 -05:00
|
|
|
ty::ty_enum(def, ref substs) => {
|
|
|
|
mywrite!(w, "t[{}|", (cx.ds)(def));
|
|
|
|
enc_substs(w, cx, substs);
|
|
|
|
mywrite!(w, "]");
|
|
|
|
}
|
2014-11-07 05:16:57 -06:00
|
|
|
ty::ty_trait(box ty::TyTrait { ref principal,
|
|
|
|
ref bounds }) => {
|
|
|
|
mywrite!(w, "x[");
|
|
|
|
enc_trait_ref(w, cx, principal);
|
2014-08-27 20:46:52 -05:00
|
|
|
enc_existential_bounds(w, cx, bounds);
|
2013-10-21 19:11:42 -05:00
|
|
|
mywrite!(w, "]");
|
|
|
|
}
|
|
|
|
ty::ty_tup(ref ts) => {
|
|
|
|
mywrite!(w, "T[");
|
|
|
|
for t in ts.iter() { enc_ty(w, cx, *t); }
|
|
|
|
mywrite!(w, "]");
|
|
|
|
}
|
2014-01-11 18:25:51 -06:00
|
|
|
ty::ty_uniq(typ) => { mywrite!(w, "~"); enc_ty(w, cx, typ); }
|
2013-10-21 19:11:42 -05:00
|
|
|
ty::ty_ptr(mt) => { mywrite!(w, "*"); enc_mt(w, cx, mt); }
|
|
|
|
ty::ty_rptr(r, mt) => {
|
|
|
|
mywrite!(w, "&");
|
|
|
|
enc_region(w, cx, r);
|
|
|
|
enc_mt(w, cx, mt);
|
|
|
|
}
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
ty::ty_vec(t, sz) => {
|
2013-10-21 19:11:42 -05:00
|
|
|
mywrite!(w, "V");
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
enc_ty(w, cx, t);
|
2014-04-09 02:15:31 -05:00
|
|
|
mywrite!(w, "/");
|
|
|
|
match sz {
|
|
|
|
Some(n) => mywrite!(w, "{}|", n),
|
|
|
|
None => mywrite!(w, "|"),
|
|
|
|
}
|
2013-10-21 19:11:42 -05:00
|
|
|
}
|
2014-04-28 20:10:23 -05:00
|
|
|
ty::ty_str => {
|
2013-10-21 19:11:42 -05:00
|
|
|
mywrite!(w, "v");
|
|
|
|
}
|
|
|
|
ty::ty_closure(ref f) => {
|
|
|
|
mywrite!(w, "f");
|
2014-07-07 18:35:15 -05:00
|
|
|
enc_closure_ty(w, cx, &**f);
|
2013-10-21 19:11:42 -05:00
|
|
|
}
|
|
|
|
ty::ty_bare_fn(ref f) => {
|
|
|
|
mywrite!(w, "F");
|
|
|
|
enc_bare_fn_ty(w, cx, f);
|
|
|
|
}
|
|
|
|
ty::ty_infer(_) => {
|
2014-02-06 03:38:08 -06:00
|
|
|
cx.diag.handler().bug("cannot encode inference variable types");
|
2013-10-21 19:11:42 -05:00
|
|
|
}
|
2014-05-31 17:53:13 -05:00
|
|
|
ty::ty_param(ParamTy {space, idx: id, def_id: did}) => {
|
|
|
|
mywrite!(w, "p{}|{}|{}|", (cx.ds)(did), id, space.to_uint())
|
2013-10-21 19:11:42 -05:00
|
|
|
}
|
|
|
|
ty::ty_struct(def, ref substs) => {
|
|
|
|
mywrite!(w, "a[{}|", (cx.ds)(def));
|
|
|
|
enc_substs(w, cx, substs);
|
|
|
|
mywrite!(w, "]");
|
|
|
|
}
|
2014-10-18 12:46:57 -05:00
|
|
|
ty::ty_unboxed_closure(def, region, ref substs) => {
|
|
|
|
mywrite!(w, "k[{}|", (cx.ds)(def));
|
2014-07-30 00:08:39 -05:00
|
|
|
enc_region(w, cx, region);
|
2014-10-18 12:46:57 -05:00
|
|
|
enc_substs(w, cx, substs);
|
|
|
|
mywrite!(w, "]");
|
2014-05-29 00:26:56 -05:00
|
|
|
}
|
2014-05-31 17:53:13 -05:00
|
|
|
ty::ty_err => {
|
|
|
|
mywrite!(w, "e");
|
|
|
|
}
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
ty::ty_open(_) => {
|
|
|
|
cx.diag.handler().bug("unexpected type in enc_sty (ty_open)");
|
|
|
|
}
|
2011-06-27 16:37:02 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-10 20:15:08 -05:00
|
|
|
|
2014-07-29 18:31:39 -05:00
|
|
|
fn enc_fn_style(w: &mut SeekableMemWriter, p: FnStyle) {
|
2012-08-06 14:34:08 -05:00
|
|
|
match p {
|
2014-04-06 20:04:40 -05:00
|
|
|
NormalFn => mywrite!(w, "n"),
|
2014-01-09 07:05:33 -06:00
|
|
|
UnsafeFn => mywrite!(w, "u"),
|
2012-05-25 01:44:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-29 18:31:39 -05:00
|
|
|
fn enc_abi(w: &mut SeekableMemWriter, abi: Abi) {
|
2013-10-21 19:11:42 -05:00
|
|
|
mywrite!(w, "[");
|
2014-04-02 03:19:41 -05:00
|
|
|
mywrite!(w, "{}", abi.name());
|
2013-10-21 19:11:42 -05:00
|
|
|
mywrite!(w, "]")
|
2013-01-31 19:12:29 -06:00
|
|
|
}
|
|
|
|
|
2014-07-29 18:31:39 -05:00
|
|
|
fn enc_onceness(w: &mut SeekableMemWriter, o: Onceness) {
|
2012-11-02 15:33:51 -05:00
|
|
|
match o {
|
2013-10-21 19:11:42 -05:00
|
|
|
Once => mywrite!(w, "o"),
|
|
|
|
Many => mywrite!(w, "m")
|
2012-11-02 15:33:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-29 18:31:39 -05:00
|
|
|
pub fn enc_bare_fn_ty(w: &mut SeekableMemWriter, cx: &ctxt, ft: &ty::BareFnTy) {
|
2014-04-06 20:04:40 -05:00
|
|
|
enc_fn_style(w, ft.fn_style);
|
2014-04-02 03:19:41 -05:00
|
|
|
enc_abi(w, ft.abi);
|
2013-01-31 19:12:29 -06:00
|
|
|
enc_fn_sig(w, cx, &ft.sig);
|
|
|
|
}
|
|
|
|
|
2014-07-29 18:31:39 -05:00
|
|
|
pub fn enc_closure_ty(w: &mut SeekableMemWriter, cx: &ctxt, ft: &ty::ClosureTy) {
|
2014-04-06 20:04:40 -05:00
|
|
|
enc_fn_style(w, ft.fn_style);
|
2013-01-31 19:12:29 -06:00
|
|
|
enc_onceness(w, ft.onceness);
|
2014-04-11 10:03:10 -05:00
|
|
|
enc_trait_store(w, cx, ft.store);
|
2014-08-27 20:46:52 -05:00
|
|
|
enc_existential_bounds(w, cx, &ft.bounds);
|
2013-01-31 19:12:29 -06:00
|
|
|
enc_fn_sig(w, cx, &ft.sig);
|
2014-05-29 00:26:56 -05:00
|
|
|
enc_abi(w, ft.abi);
|
2013-01-31 19:12:29 -06:00
|
|
|
}
|
|
|
|
|
2014-07-29 18:31:39 -05:00
|
|
|
fn enc_fn_sig(w: &mut SeekableMemWriter, cx: &ctxt, fsig: &ty::FnSig) {
|
2014-11-15 15:47:59 -06:00
|
|
|
mywrite!(w, "[");
|
2013-08-03 11:45:23 -05:00
|
|
|
for ty in fsig.inputs.iter() {
|
2013-04-26 21:13:38 -05:00
|
|
|
enc_ty(w, cx, *ty);
|
2011-06-27 16:37:02 -05:00
|
|
|
}
|
2013-10-21 19:11:42 -05:00
|
|
|
mywrite!(w, "]");
|
2013-10-25 00:56:34 -05:00
|
|
|
if fsig.variadic {
|
2013-10-29 05:03:32 -05:00
|
|
|
mywrite!(w, "V");
|
|
|
|
} else {
|
|
|
|
mywrite!(w, "N");
|
2013-10-25 00:56:34 -05:00
|
|
|
}
|
2014-10-24 14:14:37 -05:00
|
|
|
match fsig.output {
|
|
|
|
ty::FnConverging(result_type) => {
|
|
|
|
enc_ty(w, cx, result_type);
|
|
|
|
}
|
|
|
|
ty::FnDiverging => {
|
|
|
|
mywrite!(w, "z");
|
|
|
|
}
|
|
|
|
}
|
2011-06-27 16:37:02 -05:00
|
|
|
}
|
2011-07-19 19:52:34 -05:00
|
|
|
|
2014-08-27 20:46:52 -05:00
|
|
|
pub fn enc_builtin_bounds(w: &mut SeekableMemWriter, _cx: &ctxt, bs: &ty::BuiltinBounds) {
|
|
|
|
for bound in bs.iter() {
|
2013-05-07 16:30:21 -05:00
|
|
|
match bound {
|
2013-10-21 19:11:42 -05:00
|
|
|
ty::BoundSend => mywrite!(w, "S"),
|
|
|
|
ty::BoundSized => mywrite!(w, "Z"),
|
2014-03-26 18:01:11 -05:00
|
|
|
ty::BoundCopy => mywrite!(w, "P"),
|
2014-08-05 18:40:04 -05:00
|
|
|
ty::BoundSync => mywrite!(w, "T"),
|
2011-12-28 10:50:12 -06:00
|
|
|
}
|
2013-07-25 23:53:29 -05:00
|
|
|
}
|
2013-05-07 16:30:21 -05:00
|
|
|
|
2014-08-27 20:46:52 -05:00
|
|
|
mywrite!(w, ".");
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn enc_existential_bounds(w: &mut SeekableMemWriter, cx: &ctxt, bs: &ty::ExistentialBounds) {
|
|
|
|
enc_region(w, cx, bs.region_bound);
|
|
|
|
enc_builtin_bounds(w, cx, &bs.builtin_bounds);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn enc_bounds(w: &mut SeekableMemWriter, cx: &ctxt, bs: &ty::ParamBounds) {
|
|
|
|
enc_builtin_bounds(w, cx, &bs.builtin_bounds);
|
|
|
|
|
2014-08-14 17:05:27 -05:00
|
|
|
for &r in bs.region_bounds.iter() {
|
2014-08-27 20:46:52 -05:00
|
|
|
mywrite!(w, "R");
|
|
|
|
enc_region(w, cx, r);
|
|
|
|
}
|
|
|
|
|
2014-04-21 18:21:52 -05:00
|
|
|
for tp in bs.trait_bounds.iter() {
|
2013-10-21 19:11:42 -05:00
|
|
|
mywrite!(w, "I");
|
2014-04-21 18:21:52 -05:00
|
|
|
enc_trait_ref(w, cx, &**tp);
|
2013-05-07 16:30:21 -05:00
|
|
|
}
|
|
|
|
|
2013-10-21 19:11:42 -05:00
|
|
|
mywrite!(w, ".");
|
2011-12-28 10:50:12 -06:00
|
|
|
}
|
2011-06-27 16:37:02 -05:00
|
|
|
|
2014-07-29 18:31:39 -05:00
|
|
|
pub fn enc_type_param_def(w: &mut SeekableMemWriter, cx: &ctxt, v: &ty::TypeParameterDef) {
|
2014-05-31 17:53:13 -05:00
|
|
|
mywrite!(w, "{}:{}|{}|{}|",
|
2014-09-30 19:11:34 -05:00
|
|
|
token::get_name(v.name), (cx.ds)(v.def_id),
|
2014-05-31 17:53:13 -05:00
|
|
|
v.space.to_uint(), v.index);
|
2014-08-05 21:44:21 -05:00
|
|
|
enc_opt(w, v.associated_with, |w, did| mywrite!(w, "{}", (cx.ds)(did)));
|
|
|
|
mywrite!(w, "|");
|
2014-08-27 20:46:52 -05:00
|
|
|
enc_bounds(w, cx, &v.bounds);
|
2014-01-30 11:28:02 -06:00
|
|
|
enc_opt(w, v.default, |w, t| enc_ty(w, cx, t));
|
Cleanup substitutions and treatment of generics around traits in a number of ways.
- In a TraitRef, use the self type consistently to refer to the Self type:
- trait ref in `impl Trait<A,B,C> for S` has a self type of `S`.
- trait ref in `A:Trait` has the self type `A`
- trait ref associated with a trait decl has self type `Self`
- trait ref associated with a supertype has self type `Self`
- trait ref in an object type `@Trait` has no self type
- Rewrite `each_bound_traits_and_supertraits` to perform
substitutions as it goes, and thus yield a series of trait refs
that are always in the same 'namespace' as the type parameter
bound given as input. Before, we left this to the caller, but
this doesn't work because the caller lacks adequare information
to perform the type substitutions correctly.
- For provided methods, substitute the generics involved in the provided
method correctly.
- Introduce TypeParameterDef, which tracks the bounds declared on a type
parameter and brings them together with the def_id and (in the future)
other information (maybe even the parameter's name!).
- Introduce Subst trait, which helps to cleanup a lot of the
repetitive code involved with doing type substitution.
- Introduce Repr trait, which makes debug printouts far more convenient.
Fixes #4183. Needed for #5656.
2013-04-09 00:54:49 -05:00
|
|
|
}
|