To detect and signal an error on recursive types with infinite size, you need to check for cycles in the type definitions. This is particularly important when structs contain each other by value, as this creates an infinite size type (e.g., struct Foo { bar: Bar } and struct Bar { foo: Foo }).
Here's how you can implement this detection:
1. Add an Error Variant for Infinite Types
First, add a new error variant to the ErrorKind enum in ir_builder.rs to represent the case where an infinite type is detected:
[Redacted
pub enum ErrorKind {
// ... other error variants ...
InfiniteType(Vec<Str>), // Vec<Str> contains the names of the types involved in the cycle
}
2. Detect Cycles During Type Construction
You need to detect cycles during the construction of types. This can be done by keeping track of the types that are currently being constructed. If you encounter a type that is already in the process of being constructed, you've found a cycle.
Here's an example of how you might implement this in the build_ty function of the DefinedTys struct:
[Redacted
impl DefinedTys {
fn build_ty(&self, ty: &ast::Ty, program_tys: &mut TyMap, in_progress: &mut Vec<Str>) -> Result<Ty> {
use ast::TyKind as T;
match &ty.kind {
T::Name(name) => {
if in_progress.contains(&name.kind) {
// Found a cycle, return an error
return Err(Error {
kind: ErrorKind::InfiniteType(in_progress.clone()),
span: ty.span.clone(),
});
}
match self.tys.get(&name.kind) {
Some(&(ty, _)) => {
in_progress.push(name.kind.clone());
let result = Ok(ty);
in_progress.pop();
result
},
None => Err(Error {
kind: ErrorKind::NotFound("type", name.kind.clone()),
span: ty.span.clone(),
}),
}
},
// ... other type kinds ...
}
}
}
3. Update the Type Construction Pass
When constructing types in the build function, you need to pass a Vec<Str> to keep track of the types currently being constructed:
[Redacted
pub fn build(program: &ast::Program) -> Result<Program> {
// ... other code ...
// Detect infinite sized types
let mut in_progress = Vec::new();
for ast::Decl { kind, span: _ } in &program.decls {
if let D::Struct(ast::Struct { name, fields }) = kind {
in_progress.push(name.kind.clone());
for (_, field_ty) in fields {
if let Err(e) = defined_tys.build_ty(field_ty, &mut program_tys, &mut in_progress) {
return Err(e);
}
}
in_progress.pop();
}
}
// ... rest of the build function ...
}
4. Handle the Error in the Main Function
Finally, handle the InfiniteType error in the main function to display a meaningful error message to the user:
[Redacted
fn main() -> ExitCode {
// ... other code ...
let mut ir = match ir_builder::build(&ast) {
Ok(x) => x,
Err(Spanned { kind, span }) => {
use ir_builder::ErrorKind as E;
match kind {
E::InfiniteType(ty_names) => {
let mut s = "these recursive types have infinite size: ".to_owned();
s.push_str(&format!("{:?}", ty_names));
error_snippet(
Level::Error.title(&s).snippet(
Snippet::source(&src)
.origin(file_path)
.fold(true)
.annotation(Level::Error.span(span)),
),
);
return ExitCode::FAILURE;
},
// ... other error cases ...
}
}
};
// ... rest of the main function ...
}
Summary
Error Variant: Add an InfiniteType error variant to signal recursive types.
Cycle Detection: Track types currently being constructed and detect cycles.
Error Handling: Handle the error in the main function to provide a user-friendly error message.
This approach ensures that the compiler can detect and report recursive types that would lead to infinite size, helping users avoid such issues in their code.