Enum erg_compiler::varinfo::VarKind
source · pub enum VarKind {
Defined(DefId),
Declared,
Parameter {
def_id: DefId,
default: DefaultInfo,
},
Auto,
FixedAuto,
DoesNotExist,
Builtin,
}
Variants§
Implementations§
source§impl VarKind
impl VarKind
sourcepub const fn parameter(def_id: DefId, default: DefaultInfo) -> Self
pub const fn parameter(def_id: DefId, default: DefaultInfo) -> Self
Examples found in repository?
context/mod.rs (line 525)
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
pub fn with_capacity(
name: Str,
cfg: ErgConfig,
kind: ContextKind,
params: Vec<ParamSpec>,
outer: Option<Context>,
mod_cache: Option<SharedModuleCache>,
py_mod_cache: Option<SharedModuleCache>,
capacity: usize,
level: usize,
) -> Self {
let mut params_ = Vec::new();
for param in params.into_iter() {
let id = DefId(get_hash(&(&name, ¶m)));
if let Some(name) = param.name {
let kind = VarKind::parameter(id, param.default_info);
let muty = Mutability::from(name);
let vi = VarInfo::new(param.t, muty, Private, kind, None, None, None);
params_.push((Some(VarName::new(Token::static_symbol(name))), vi));
} else {
let kind = VarKind::parameter(id, param.default_info);
let muty = Mutability::Immutable;
let vi = VarInfo::new(param.t, muty, Private, kind, None, None, None);
params_.push((None, vi));
}
}
Self {
name,
cfg,
kind,
preds: vec![],
outer: outer.map(Box::new),
super_classes: vec![],
super_traits: vec![],
methods_list: vec![],
const_param_defaults: Dict::default(),
method_to_traits: Dict::default(),
method_to_classes: Dict::default(),
method_impl_patches: Dict::default(),
trait_impls: Dict::default(),
params: params_,
decls: Dict::default(),
future_defined_locals: Dict::default(),
deleted_locals: Dict::default(),
locals: Dict::with_capacity(capacity),
consts: Dict::default(),
mono_types: Dict::default(),
poly_types: Dict::default(),
mod_cache,
py_mod_cache,
tv_cache: None,
patches: Dict::default(),
level,
}
}
More examples
context/register.rs (lines 247-250)
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
fn assign_param(
&mut self,
sig: &ast::NonDefaultParamSignature,
default_val_exists: bool,
opt_decl_t: Option<&ParamTy>,
) -> TyCheckResult<()> {
let vis = if self.cfg.python_compatible_mode {
Public
} else {
Private
};
match &sig.pat {
// Literal patterns will be desugared to discard patterns
ast::ParamPattern::Lit(_) => unreachable!(),
ast::ParamPattern::Discard(_token) => {
let spec_t = self.instantiate_param_sig_t(
sig,
opt_decl_t,
&mut TyVarCache::new(self.level, self),
Normal,
)?;
let kind = VarKind::parameter(
DefId(get_hash(&(&self.name, "_"))),
DefaultInfo::NonDefault,
);
self.params.push((
Some(VarName::from_static("_")),
VarInfo::new(spec_t, Immutable, vis, kind, None, None, None),
));
Ok(())
}
ast::ParamPattern::VarName(name) => {
if self
.registered_info(name.inspect(), name.is_const())
.is_some()
{
Err(TyCheckErrors::from(TyCheckError::reassign_error(
self.cfg.input.clone(),
line!() as usize,
name.loc(),
self.caused_by(),
name.inspect(),
)))
} else {
// ok, not defined
let mut dummy_tv_cache = TyVarCache::new(self.level, self);
let spec_t =
self.instantiate_param_sig_t(sig, opt_decl_t, &mut dummy_tv_cache, Normal)?;
if &name.inspect()[..] == "self" {
if let Some(self_t) = self.rec_get_self_t() {
self.sub_unify(&spec_t, &self_t, name.loc(), Some(name.inspect()))?;
} else {
log!(err "self_t is None");
}
}
let default = if default_val_exists {
DefaultInfo::WithDefault
} else {
DefaultInfo::NonDefault
};
let kind = VarKind::parameter(DefId(get_hash(&(&self.name, name))), default);
let muty = Mutability::from(&name.inspect()[..]);
self.params.push((
Some(name.clone()),
VarInfo::new(spec_t, muty, vis, kind, None, None, None),
));
Ok(())
}
}
ast::ParamPattern::Ref(name) => {
if self
.registered_info(name.inspect(), name.is_const())
.is_some()
{
Err(TyCheckErrors::from(TyCheckError::reassign_error(
self.cfg.input.clone(),
line!() as usize,
name.loc(),
self.caused_by(),
name.inspect(),
)))
} else {
// ok, not defined
let mut dummy_tv_cache = TyVarCache::new(self.level, self);
let spec_t =
self.instantiate_param_sig_t(sig, opt_decl_t, &mut dummy_tv_cache, Normal)?;
if &name.inspect()[..] == "self" {
if let Some(self_t) = self.rec_get_self_t() {
self.sub_unify(&spec_t, &self_t, name.loc(), Some(name.inspect()))?;
} else {
log!(err "self_t is None");
}
}
let spec_t = ref_(spec_t);
let default = if default_val_exists {
DefaultInfo::WithDefault
} else {
DefaultInfo::NonDefault
};
let kind = VarKind::parameter(DefId(get_hash(&(&self.name, name))), default);
self.params.push((
Some(name.clone()),
VarInfo::new(spec_t, Immutable, vis, kind, None, None, None),
));
Ok(())
}
}
ast::ParamPattern::RefMut(name) => {
if self
.registered_info(name.inspect(), name.is_const())
.is_some()
{
Err(TyCheckErrors::from(TyCheckError::reassign_error(
self.cfg.input.clone(),
line!() as usize,
name.loc(),
self.caused_by(),
name.inspect(),
)))
} else {
// ok, not defined
let mut dummy_tv_cache = TyVarCache::new(self.level, self);
let spec_t =
self.instantiate_param_sig_t(sig, opt_decl_t, &mut dummy_tv_cache, Normal)?;
if &name.inspect()[..] == "self" {
if let Some(self_t) = self.rec_get_self_t() {
self.sub_unify(&spec_t, &self_t, name.loc(), Some(name.inspect()))?;
} else {
log!(err "self_t is None");
}
}
let spec_t = ref_mut(spec_t.clone(), Some(spec_t));
let default = if default_val_exists {
DefaultInfo::WithDefault
} else {
DefaultInfo::NonDefault
};
let kind = VarKind::parameter(DefId(get_hash(&(&self.name, name))), default);
self.params.push((
Some(name.clone()),
VarInfo::new(spec_t, Immutable, vis, kind, None, None, None),
));
Ok(())
}
}
other => {
log!(err "{other}");
unreachable!("{other}")
}
}
}
sourcepub const fn has_default(&self) -> bool
pub const fn has_default(&self) -> bool
Examples found in repository?
lower.rs (line 1009)
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
fn lower_lambda(&mut self, lambda: ast::Lambda) -> LowerResult<hir::Lambda> {
log!(info "entered {}({lambda})", fn_name!());
let is_procedural = lambda.is_procedural();
let id = get_hash(&lambda.sig);
let name = format!("<lambda_{id}>");
let kind = if is_procedural {
ContextKind::Proc
} else {
ContextKind::Func
};
let tv_cache = self
.ctx
.instantiate_ty_bounds(&lambda.sig.bounds, RegistrationMode::Normal)?;
self.ctx.grow(&name, kind, Private, Some(tv_cache));
let params = self.lower_params(lambda.sig.params).map_err(|errs| {
self.pop_append_errs();
errs
})?;
if let Err(errs) = self.ctx.assign_params(¶ms, None) {
self.errs.extend(errs);
}
if let Err(errs) = self.ctx.preregister(&lambda.body) {
self.errs.extend(errs);
}
let body = self.lower_block(lambda.body).map_err(|e| {
self.pop_append_errs();
e
})?;
let (non_default_params, default_params): (Vec<_>, Vec<_>) = self
.ctx
.params
.iter()
.partition(|(_, v)| !v.kind.has_default());
let non_default_params = non_default_params
.into_iter()
.map(|(name, vi)| {
ParamTy::pos(name.as_ref().map(|n| n.inspect().clone()), vi.t.clone())
})
.collect();
let default_params = default_params
.into_iter()
.map(|(name, vi)| ParamTy::kw(name.as_ref().unwrap().inspect().clone(), vi.t.clone()))
.collect();
self.pop_append_errs();
let t = if is_procedural {
proc(non_default_params, None, default_params, body.t())
} else {
func(non_default_params, None, default_params, body.t())
};
let t = if t.has_qvar() { t.quantify() } else { t };
Ok(hir::Lambda::new(id, params, lambda.op, body, t))
}