Skip to content

Commit

Permalink
fix(compiler): Better #line attribute generation (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
tibordp authored Oct 13, 2024
1 parent 8a621ba commit a8aca1b
Show file tree
Hide file tree
Showing 16 changed files with 105 additions and 78 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ jobs:
- name: Install Tree-sitter (with runtime)
run: |
npm install -g tree-sitter-cli
curl -fsSL https://github.com/tree-sitter/tree-sitter/archive/refs/tags/v0.23.0.tar.gz | tar -xz
curl -fsSL https://github.com/tree-sitter/tree-sitter/archive/refs/tags/v0.24.2.tar.gz | tar -xz
cd tree-sitter-*
make
sudo make install
Expand Down Expand Up @@ -146,7 +146,7 @@ jobs:
- name: Install Tree-sitter (with runtime)
run: |
npm install -g tree-sitter-cli
curl -fsSL https://github.com/tree-sitter/tree-sitter/archive/refs/tags/v0.23.0.tar.gz | tar -xz
curl -fsSL https://github.com/tree-sitter/tree-sitter/archive/refs/tags/v0.24.2.tar.gz | tar -xz
cd tree-sitter-*
make
sudo make install
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Install Tree-sitter (with runtime)
run: |
npm install -g tree-sitter-cli
curl -fsSL https://github.com/tree-sitter/tree-sitter/archive/refs/tags/v0.23.0.tar.gz | tar -xz
curl -fsSL https://github.com/tree-sitter/tree-sitter/archive/refs/tags/v0.24.2.tar.gz | tar -xz
cd tree-sitter-*
make -j8
sudo make install
Expand Down
55 changes: 31 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ RUN mkdir -p /etc/apt/keyrings && \
RUN cargo install tree-sitter-cli

WORKDIR /alumina/deps
RUN curl -fsSL https://github.com/tree-sitter/tree-sitter/archive/refs/tags/v0.23.0.tar.gz | tar -xz
RUN curl -fsSL https://github.com/tree-sitter/tree-sitter/archive/refs/tags/v0.24.2.tar.gz | tar -xz
RUN cd tree-sitter-* && make -j8 && make install && ldconfig
RUN curl -fsSL https://github.com/ianlancetaylor/libbacktrace/archive/master.tar.gz | tar -xz
RUN cd libbacktrace-* && ./configure && make -j8 && make install
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ endif
## ----------------------------- Minicoro ------------------------------

$(BUILD_DIR)/minicoro.o: common/minicoro/minicoro.h
$(CC) $(CFLAGS) -DMINICORO_IMPL -xc -c $^ -o $@
$(CC) $(CFLAGS) -DMINICORO_IMPL -DNDEBUG -xc -c $^ -o $@

## --------------------------- Stdlib tests ----------------------------

Expand Down
7 changes: 4 additions & 3 deletions docs/lang_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -1374,7 +1374,7 @@ impl FancyInt {
}

fn sum<T: Additive<T>>(slice: &[T]) -> T {
let mut result = T::zero();
let result = T::zero();
for item in slice {
result = result.add(item);
}
Expand Down Expand Up @@ -1794,9 +1794,10 @@ mod tests {
}

#[test]
#[test::should_panic]
fn test_panic() {
panic!("oops");
test::assert_panics!({
panic!("oops");
});
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion src/alumina-boot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ clap = { version = "4", features = ["derive", "env"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
alumina-boot-macros = { path = "../alumina-boot-macros" }
tree-sitter = "0.23.0"
tree-sitter = "0.24.2"
bumpalo = "3"
thiserror = "1"
indexmap = "2"
Expand Down
1 change: 1 addition & 0 deletions src/alumina-boot/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,7 @@ pub enum Attribute<'ast> {
Packed(usize),
TupleCall,
ConstOnly,
ReturnsTwice,
NoConst,
Diagnostic(Diagnostic),
Transparent,
Expand Down
51 changes: 37 additions & 14 deletions src/alumina-boot/src/codegen/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ pub fn write_function_signature<'ir, 'gen>(
attributes = format!("__attribute__((cold)) {}", attributes);
}

if item.attributes.contains(&Attribute::ReturnsTwice) {
attributes = format!("__attribute__((returns_twice)) {}", attributes);
}

if item.return_type.is_never() {
attributes = format!("_Noreturn {}", attributes);
}
Expand Down Expand Up @@ -311,6 +315,18 @@ impl<'ir, 'gen> FunctionWriter<'ir, 'gen> {
Ok(())
}

fn write_line_directive(&mut self, span: Span, trailing_endline: bool) {
if let Some(filename) = self.ctx.global_ctx.diag().get_file_path(span.file) {
w!(
self.fn_bodies,
"\n#line {} {:?}{}",
span.line + 1,
filename.display(),
if trailing_endline { "\n" } else { "" }
);
}
}

pub fn write_expr(
&mut self,
diag: &DiagnosticsStack,
Expand All @@ -323,23 +339,17 @@ impl<'ir, 'gen> FunctionWriter<'ir, 'gen> {

if self.debug_info {
if let Some(span) = expr.span.map(|s| self.ctx.global_ctx.diag().map_span(s)) {
let prev_line = self.last_span.map(|s| (s.file, s.line + 1));
if prev_line != Some((span.file, span.line + 1)) {
if prev_line == Some((span.file, span.line)) {
match self.last_span {
Some(s) if s.file == span.file && s.line == span.line => {}
Some(s) if s.file == span.file && s.line + 1 == span.line => {
w!(self.fn_bodies, "\n");
} else if let Some(filename) =
self.ctx.global_ctx.diag().get_file_path(span.file)
{
w!(
self.fn_bodies,
"\n#line {} {:?}\n",
span.line + 1,
filename.display()
);
}
} else {
self.last_span = None;
_ => self.write_line_directive(span, true),
}

self.last_span = Some(span);
} else {
self.last_span = None;
}
}

Expand Down Expand Up @@ -764,6 +774,13 @@ impl<'ir, 'gen> FunctionWriter<'ir, 'gen> {
item: &'ir Const<'ir>,
) -> Result<(), AluminaError> {
let _guard = diag.push_span(item.span);

if self.debug_info {
if let Some(span) = item.span.map(|s| self.ctx.global_ctx.diag().map_span(s)) {
self.write_line_directive(span, false);
}
}

w!(
self.fn_bodies,
"\nconst static {} {} = ",
Expand Down Expand Up @@ -796,6 +813,12 @@ impl<'ir, 'gen> FunctionWriter<'ir, 'gen> {
return Ok(());
}

if self.debug_info {
if let Some(span) = item.span.map(|s| self.ctx.global_ctx.diag().map_span(s)) {
self.write_line_directive(span, false);
}
}

write_function_signature(
self.ctx,
&mut self.fn_bodies,
Expand Down
1 change: 1 addition & 0 deletions src/alumina-boot/src/ir/mono/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ impl<'ast, 'ir> MonoCtx<'ast, 'ir> {
Attribute::LinkName(s) => Attribute::LinkName(s.alloc_on(self.ir)),
Attribute::Coroutine => Attribute::Coroutine,
Attribute::Custom(v) => Attribute::Custom(self.map_custom_attribute(v)),
Attribute::ReturnsTwice => Attribute::ReturnsTwice,
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/alumina-boot/src/visitors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,10 @@ impl<'ast, 'src> AluminaVisitor<'src> for AttributeVisitor<'ast, 'src> {
check_duplicate!(Attribute::Export);
self.attributes.push(Attribute::Export);
}
"returns_twice" => {
check_duplicate!(Attribute::ReturnsTwice);
self.attributes.push(Attribute::ReturnsTwice);
}
"thread_local" => {
check_duplicate!(Attribute::ThreadLocal);
// We can skip thread-local on programs that are compiled with threads
Expand Down
File renamed without changes.
12 changes: 6 additions & 6 deletions sysroot/std/cmp.alu
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ protocol DefaultEquatable<Self> {
}
} else {
compile_fail!(
"field {} is not automatically equatable",
ty.fields().(i).name()
"field {} of {} is not equated automatically",
ty.fields().(i).name(), ty.debug_name()
);
}
}
Expand All @@ -98,7 +98,7 @@ protocol DefaultEquatable<Self> {
} else when ty.is_enum() {
lhs == rhs
} else {
compile_fail!("type cannot be automatically hashed")
compile_fail!("type {} cannot be equated automatically", ty.debug_name());
}
}

Expand Down Expand Up @@ -244,15 +244,15 @@ protocol LexicographicComparable<Self> {
}
} else {
compile_fail!(
"field {} is not automatically comparable",
ty.fields().(i).name()
"field {} of {} cannot be compared automatically",
ty.fields().(i).name(), ty.debug_name()
);
}
}

Ordering::Equal
} else {
compile_fail!("type cannot be lexicographically compared")
compile_fail!("type {} cannot be compared automatically", ty.debug_name());
}
}

Expand Down
Loading

0 comments on commit a8aca1b

Please sign in to comment.