[meta] Make Typevar::type_set private, and add a second getter to read it directly;

There might be a silent bug in the Python module which directly reads
from this type_set field; in particular, it does so when reading all
controlling type variables, which all seem to be free variables (i.e. no
parent typevar). So imitate this behavior here, until we're sure there
are no other meta generators that rely on this.
This commit is contained in:
Benjamin Bouvier
2019-03-29 17:00:02 +01:00
parent 5edee84f67
commit 2dd1552369

View File

@@ -25,7 +25,9 @@ pub struct TypeVarContent {
pub doc: String, pub doc: String,
/// Type set associated to the type variable. /// Type set associated to the type variable.
pub type_set: Rc<TypeSet>, /// This field must remain private; use `get_typeset()` or `get_raw_typeset()` to get the
/// information you want.
type_set: Rc<TypeSet>,
pub base: Option<TypeVarParent>, pub base: Option<TypeVarParent>,
} }
@@ -84,13 +86,22 @@ impl TypeVar {
TypeVar::new(name, doc, builder.finish()) TypeVar::new(name, doc, builder.finish())
} }
/// Returns this typevar's type set, maybe computing it from the parent.
fn get_typeset(&self) -> Rc<TypeSet> { fn get_typeset(&self) -> Rc<TypeSet> {
// TODO Can this be done in a non-lazy way in derived() and we can remove this function and
// the one below?
match &self.content.base { match &self.content.base {
Some(base) => Rc::new(base.type_var.get_typeset().image(base.derived_func)), Some(base) => Rc::new(base.type_var.get_typeset().image(base.derived_func)),
None => self.content.type_set.clone(), None => self.content.type_set.clone(),
} }
} }
/// Returns this typevar's type set, assuming this type var has no parent.
pub fn get_raw_typeset(&self) -> &TypeSet {
assert_eq!(self.content.type_set, self.get_typeset());
&*self.content.type_set
}
/// If the associated typeset has a single type return it. Otherwise return None. /// If the associated typeset has a single type return it. Otherwise return None.
pub fn singleton_type(&self) -> Option<ValueType> { pub fn singleton_type(&self) -> Option<ValueType> {
let type_set = self.get_typeset(); let type_set = self.get_typeset();