Skip to content

Commit

Permalink
WIP: move Placeholder into TypeId
Browse files Browse the repository at this point in the history
  • Loading branch information
yorickpeterse committed Dec 8, 2023
1 parent 08a17d1 commit 835124c
Show file tree
Hide file tree
Showing 9 changed files with 348 additions and 435 deletions.
2 changes: 1 addition & 1 deletion compiler/src/llvm/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl Context {
layouts: &Layouts<'a>,
type_ref: TypeRef,
) -> BasicTypeEnum<'a> {
if let Ok(id) = type_ref.type_id(db) {
if let Some(id) = type_ref.type_id() {
let base = match id {
TypeId::Foreign(ForeignType::Int(8, _)) => {
self.i8_type().as_basic_type_enum()
Expand Down
18 changes: 9 additions & 9 deletions compiler/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -992,30 +992,30 @@ pub(crate) enum CastType {
}

impl CastType {
fn from(db: &Database, typ: TypeRef) -> CastType {
fn from(typ: TypeRef) -> CastType {
if let TypeRef::Pointer(_) = typ {
CastType::Pointer
} else {
match typ.type_id(db) {
Ok(TypeId::Foreign(ForeignType::Int(8, signed))) => {
match typ.type_id() {
Some(TypeId::Foreign(ForeignType::Int(8, signed))) => {
CastType::Int(8, signed)
}
Ok(TypeId::Foreign(ForeignType::Int(16, signed))) => {
Some(TypeId::Foreign(ForeignType::Int(16, signed))) => {
CastType::Int(16, signed)
}
Ok(TypeId::Foreign(ForeignType::Int(32, signed))) => {
Some(TypeId::Foreign(ForeignType::Int(32, signed))) => {
CastType::Int(32, signed)
}
Ok(TypeId::Foreign(ForeignType::Int(64, signed))) => {
Some(TypeId::Foreign(ForeignType::Int(64, signed))) => {
CastType::Int(64, signed)
}
Ok(TypeId::Foreign(ForeignType::Float(32))) => {
Some(TypeId::Foreign(ForeignType::Float(32))) => {
CastType::Float(32)
}
Ok(TypeId::Foreign(ForeignType::Float(64))) => {
Some(TypeId::Foreign(ForeignType::Float(64))) => {
CastType::Float(64)
}
Ok(TypeId::ClassInstance(ins)) => match ins.instance_of().0 {
Some(TypeId::ClassInstance(ins)) => match ins.instance_of().0 {
INT_ID | NIL_ID | BOOL_ID => CastType::Int(64, true),
FLOAT_ID => CastType::Float(64),
_ => CastType::Object,
Expand Down
5 changes: 1 addition & 4 deletions compiler/src/mir/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2385,10 +2385,7 @@ impl<'a> LowerMethod<'a> {
let from_type = self.register_type(src);
let to_type = node.resolved_type;

match (
CastType::from(self.db(), from_type),
CastType::from(self.db(), to_type),
) {
match (CastType::from(from_type), CastType::from(to_type)) {
(CastType::Object, CastType::Object) => {
let out = self.input_register(src, from_type, None, loc);

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/mir/pattern_matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ impl<'a> Compiler<'a> {

fn variable_type(&mut self, variable: &Variable) -> Type {
let typ = variable.value_type(&self.variables);
let type_id = typ.type_id(self.db()).unwrap();
let type_id = typ.type_id().unwrap();
let class_ins = if let TypeId::ClassInstance(ins) = type_id {
ins
} else {
Expand Down
8 changes: 4 additions & 4 deletions compiler/src/mir/specialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ impl<'a, 'b> Specialize<'a, 'b> {
Instruction::CallDynamic(call) => match method
.registers
.value_type(call.receiver)
.as_class_instance(&self.state.db)
.as_class_instance()
{
// As part of specialization, we may encounter a dynamic
// call that's now acting on a class instance. We need
Expand Down Expand Up @@ -454,8 +454,8 @@ impl<'a, 'b> Specialize<'a, 'b> {
// As a result of specialization we may need to change
// the cast types, such as when a type parameter is
// specialized as an Int.
ins.from = CastType::from(&self.state.db, from);
ins.to = CastType::from(&self.state.db, to);
ins.from = CastType::from(from);
ins.to = CastType::from(to);
}
_ => {}
}
Expand Down Expand Up @@ -610,7 +610,7 @@ impl<'a, 'b> Specialize<'a, 'b> {
) -> (MethodId, Vec<Shape>) {
let trait_id = method
.receiver(&self.state.db)
.as_trait_instance(&self.state.db)
.as_trait_instance()
.unwrap()
.instance_of();

Expand Down
42 changes: 24 additions & 18 deletions compiler/src/type_check/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ impl MethodCall {
// parameters), as that would involve copying lots of data structures,
// and because it complicates looking up type parameters, so instead we
// handle it here when/if necessary.
if receiver.is_rigid_type_parameter(&state.db) {
if receiver.is_rigid_type_parameter() {
for val in type_arguments.values_mut() {
*val = match val {
TypeRef::Any(TypeId::TypeParameter(id)) => {
Expand Down Expand Up @@ -577,7 +577,7 @@ impl MethodCall {

fn resolve_return_type(&mut self, state: &mut State) -> TypeRef {
let raw = self.method.return_type(&state.db);
let rigid = self.receiver.is_rigid_type_parameter(&state.db);
let rigid = self.receiver.is_rigid_type_parameter();

self.return_type = TypeResolver::new(
&mut state.db,
Expand Down Expand Up @@ -1170,13 +1170,16 @@ impl<'a> CheckConstant<'a> {
name: &str,
location: &SourceLocation,
) -> Option<(TypeId, MethodId)> {
let rec_id = match receiver.type_id(self.db()) {
Ok(id) => id,
Err(TypeRef::Error) => return None,
Err(typ) => {
if receiver.is_error(self.db()) {
return None;
}

let rec_id = match receiver.type_id() {
Some(id) => id,
_ => {
self.state.diagnostics.undefined_method(
name,
format_type(self.db(), typ),
format_type(self.db(), receiver),
self.file(),
location.clone(),
);
Expand Down Expand Up @@ -1439,7 +1442,7 @@ impl<'a> CheckMethodBody<'a> {
match node {
hir::Expression::Closure(ref mut n) => {
let expected = expected_type
.closure_id(self.db())
.closure_id()
.map(|f| (f, expected_type, type_arguments));

self.closure(n, expected, scope)
Expand Down Expand Up @@ -2599,7 +2602,7 @@ impl<'a> CheckMethodBody<'a> {
let module = self.module;
let (rec, rec_id, rec_kind, method) = {
let rec = scope.surrounding_type;
let rec_id = rec.type_id(self.db()).unwrap();
let rec_id = rec.type_id().unwrap();

match rec_id.lookup_method(self.db(), &node.name, module, false) {
MethodLookup::Ok(method) => {
Expand Down Expand Up @@ -2715,7 +2718,7 @@ impl<'a> CheckMethodBody<'a> {

let (rec, rec_id, rec_kind, method) = {
let rec = scope.surrounding_type;
let rec_id = rec.type_id(self.db()).unwrap();
let rec_id = rec.type_id().unwrap();

match rec_id.lookup_method(self.db(), name, module, true) {
MethodLookup::Ok(method) if method.is_extern(self.db()) => {
Expand Down Expand Up @@ -3516,7 +3519,7 @@ impl<'a> CheckMethodBody<'a> {
if let Some((rec, allow_type_private)) =
node.receiver.as_mut().map(|r| self.call_receiver(r, scope))
{
if let Some(closure) = rec.closure_id(self.db()) {
if let Some(closure) = rec.closure_id() {
self.call_closure(rec, closure, node, scope)
} else {
self.call_with_receiver(
Expand Down Expand Up @@ -3772,7 +3775,7 @@ impl<'a> CheckMethodBody<'a> {
let name = &node.name.name;
let module = self.module;
let rec = scope.surrounding_type;
let rec_id = rec.type_id(self.db()).unwrap();
let rec_id = rec.type_id().unwrap();
let (rec_info, rec, rec_id, method) =
match rec_id.lookup_method(self.db(), name, module, true) {
MethodLookup::Ok(method) => {
Expand Down Expand Up @@ -4091,10 +4094,12 @@ impl<'a> CheckMethodBody<'a> {
receiver: TypeRef,
location: &SourceLocation,
) -> Option<TypeId> {
match receiver.type_id(self.db()) {
Ok(id) => Some(id),
Err(TypeRef::Error) => None,
Err(TypeRef::Placeholder(_)) => {
if receiver.is_error(self.db()) {
return None;
}

match receiver.type_id() {
Some(TypeId::Placeholder(id)) if id.value(self.db()).is_none() => {
self.state.diagnostics.cant_infer_type(
format_type(self.db(), receiver),
self.file(),
Expand All @@ -4103,12 +4108,13 @@ impl<'a> CheckMethodBody<'a> {

None
}
Err(typ) => {
Some(id) => Some(id),
_ => {
self.state.diagnostics.error(
DiagnosticId::InvalidCall,
format!(
"methods can't be called on values of type '{}'",
self.fmt(typ)
self.fmt(receiver)
),
self.file(),
location.clone(),
Expand Down
Loading

0 comments on commit 835124c

Please sign in to comment.