core: support raise exception short form

This commit is contained in:
abdul124 2024-07-10 17:34:40 +08:00
parent d658d9b00e
commit 52e5d417ea
2 changed files with 27 additions and 2 deletions

View File

@ -2457,7 +2457,29 @@ pub fn gen_expr<'ctx, G: CodeGenerator>(
Some((_, Some(static_value), _)) => ValueEnum::Static(static_value.clone()), Some((_, Some(static_value), _)) => ValueEnum::Static(static_value.clone()),
None => { None => {
let resolver = ctx.resolver.clone(); let resolver = ctx.resolver.clone();
resolver.get_symbol_value(*id, ctx).unwrap() if let Some(res) = resolver.get_symbol_value(*id, ctx) {
res
} else {
// Allow "raise Exception" short form
let def_id = resolver.get_identifier_def(*id).map_err(|e| {
format!("{} (at {})", e.iter().next().unwrap(), expr.location)
})?;
let def = ctx.top_level.definitions.read();
if let TopLevelDef::Class { constructor, .. } = *def[def_id.0].read() {
let TypeEnum::TFunc(signature) =
ctx.unifier.get_ty(constructor.unwrap()).as_ref().clone()
else {
return Err(format!(
"Failed to resolve symbol {} (at {})",
id, expr.location
));
};
return Ok(generator
.gen_call(ctx, None, (&signature, def_id), Vec::default())?
.map(Into::into));
}
return Err(format!("Failed to resolve symbol {} (at {})", id, expr.location));
}
} }
}, },
ExprKind::List { elts, .. } => { ExprKind::List { elts, .. } => {

View File

@ -398,7 +398,10 @@ impl<'a> Fold<()> for Inferencer<'a> {
} }
if let Some(exc) = exc { if let Some(exc) = exc {
self.virtual_checks.push(( self.virtual_checks.push((
exc.custom.unwrap(), match &*self.unifier.get_ty(exc.custom.unwrap()) {
TypeEnum::TFunc(sign) => sign.ret,
_ => exc.custom.unwrap(),
},
self.primitives.exception, self.primitives.exception,
exc.location, exc.location,
)); ));