-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #24209 - nikomatsakis:refactor-unification, r=nrc
I'm on a quest to slowly refactor a lot of the inference code. A first step for that is moving the "pure data structures" out so as to simplify what's left. This PR moves `snapshot_vec`, `graph`, and `unify` into their own crate (`librustc_data_structures`). They can then be unit-tested, benchmarked, etc more easily. As a benefit, I improved the performance of unification slightly on the benchmark I added vs the original code. r? @nrc
- Loading branch information
Showing
18 changed files
with
782 additions
and
382 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT | ||
// 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. | ||
|
||
use middle::ty::{self, IntVarValue, Ty}; | ||
use rustc_data_structures::unify::UnifyKey; | ||
use syntax::ast; | ||
|
||
pub trait ToType<'tcx> { | ||
fn to_type(&self, tcx: &ty::ctxt<'tcx>) -> Ty<'tcx>; | ||
} | ||
|
||
impl UnifyKey for ty::IntVid { | ||
type Value = Option<IntVarValue>; | ||
fn index(&self) -> u32 { self.index } | ||
fn from_index(i: u32) -> ty::IntVid { ty::IntVid { index: i } } | ||
fn tag(_: Option<ty::IntVid>) -> &'static str { "IntVid" } | ||
} | ||
|
||
impl<'tcx> ToType<'tcx> for IntVarValue { | ||
fn to_type(&self, tcx: &ty::ctxt<'tcx>) -> Ty<'tcx> { | ||
match *self { | ||
ty::IntType(i) => ty::mk_mach_int(tcx, i), | ||
ty::UintType(i) => ty::mk_mach_uint(tcx, i), | ||
} | ||
} | ||
} | ||
|
||
// Floating point type keys | ||
|
||
impl UnifyKey for ty::FloatVid { | ||
type Value = Option<ast::FloatTy>; | ||
fn index(&self) -> u32 { self.index } | ||
fn from_index(i: u32) -> ty::FloatVid { ty::FloatVid { index: i } } | ||
fn tag(_: Option<ty::FloatVid>) -> &'static str { "FloatVid" } | ||
} | ||
|
||
impl<'tcx> ToType<'tcx> for ast::FloatTy { | ||
fn to_type(&self, tcx: &ty::ctxt<'tcx>) -> Ty<'tcx> { | ||
ty::mk_mach_float(tcx, *self) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT | ||
// 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. | ||
|
||
use std::iter; | ||
|
||
/// A very simple BitVector type. | ||
pub struct BitVector { | ||
data: Vec<u64> | ||
} | ||
|
||
impl BitVector { | ||
pub fn new(num_bits: usize) -> BitVector { | ||
let num_words = (num_bits + 63) / 64; | ||
BitVector { data: iter::repeat(0).take(num_words).collect() } | ||
} | ||
|
||
fn word_mask(&self, bit: usize) -> (usize, u64) { | ||
let word = bit / 64; | ||
let mask = 1 << (bit % 64); | ||
(word, mask) | ||
} | ||
|
||
pub fn contains(&self, bit: usize) -> bool { | ||
let (word, mask) = self.word_mask(bit); | ||
(self.data[word] & mask) != 0 | ||
} | ||
|
||
pub fn insert(&mut self, bit: usize) -> bool { | ||
let (word, mask) = self.word_mask(bit); | ||
let data = &mut self.data[word]; | ||
let value = *data; | ||
*data = value | mask; | ||
(value | mask) != value | ||
} | ||
} |
Oops, something went wrong.