Skip to content

fix(rust): use core crates for no_std#4303

Merged
clason merged 5 commits intotree-sitter:masterfrom
Tiggilyboo:no-std-fix
Mar 25, 2025
Merged

fix(rust): use core crates for no_std#4303
clason merged 5 commits intotree-sitter:masterfrom
Tiggilyboo:no-std-fix

Conversation

@Tiggilyboo
Copy link
Contributor

@Tiggilyboo Tiggilyboo commented Mar 21, 2025

Fixes #4302

Adds a feature flag to the binding lib, used core where possible. Maybe worth a no_std test case?

@Tiggilyboo Tiggilyboo changed the title Fix use of std, no_std cannot do dot graphs Fix use of std when feature is not present Mar 21, 2025
@WillLillis
Copy link
Member

I tried applying your patch locally but still got a compile error related to the Drop implementation for Parser:

error[E0599]: no method named `stop_printing_dot_graphs` found
    --> /home/lillis/projects/tree-sitter/lib/binding_rust/lib
.rs:1435:14
     |
1435 |         self.stop_printing_dot_graphs();
     |              ^^^^^^^^^^^^^^^^^^^^^^^^ method not found 
in `&mut Parser`

For more information about this error, try `rustc --explain E0
599`.
error: could not compile `tree-sitter` (lib) due to 1 previous
 error

It looks like locking the call to stop_printing_dot_graphs behind the std feature fixes things:

diff --git a/lib/binding_rust/lib.rs b/lib/binding_rust/lib.rs
index 62839d9b..8577fb1d 100644
--- a/lib/binding_rust/lib.rs
+++ b/lib/binding_rust/lib.rs
@@ -1432,7 +1432,10 @@ impl Parser {

 impl Drop for Parser {
     fn drop(&mut self) {
-        self.stop_printing_dot_graphs();
+        #[cfg(feature = "std")]
+        {
+            self.stop_printing_dot_graphs();
+        }
         self.set_logger(None);
         unsafe { ffi::ts_parser_delete(self.0.as_ptr()) }
     }

Maybe worth a no_std test case?

A new test case would be welcome, but could also be taken care of in a followup.

@ObserverOfTime ObserverOfTime changed the title Fix use of std when feature is not present fix(rust): use core crates for no_std Mar 22, 2025
@clason
Copy link
Contributor

clason commented Mar 22, 2025

A new test case would be welcome, but could also be taken care of in a followup.

I would strongly encourage adding a test now. Otherwise it'll likely never happen and things can easily backslide. (In fact, I would always start with the test so you know which changes to make to have it work.)

@Tiggilyboo
Copy link
Contributor Author

I've pushed the above fix for the call to stop_printing_dot_graphs, thanks for that - however I'm unsure of how go about setting up the no_std test case. The repo has a Dockerfile set up to test with CMD cargo test --all-features, could it be as easy as just running cargo test --no-default-features afterward?

Just looking for some direction on how this should be acheived, cheers!

@WillLillis
Copy link
Member

WillLillis commented Mar 22, 2025

cargo test --no-default-features still works for me locally without this patch, so I don't think that's the way to go. What about something like this added to .github/workflows/build.yml?

    - name: Check no_std
      if: ${{ !matrix.no-run && inputs.run-test }}
      run: |
        cd ..
        cargo new no_std_check
        cd no_std_check
        echo "tree-sitter = { path = \"../tree-sitter/lib/\", default-features = false, features = [] }" >> Cargo.toml
        cargo build

CC @ObserverOfTime?

@ObserverOfTime
Copy link
Member

What about cargo build --no-default-features ?

@WillLillis
Copy link
Member

What about cargo build --no-default-features ?

That succeeds when it's run from the root directory, but fails with the expected errors when I run it inside lib/. I'm not sure why it succeeds in the former case, but that should work regardless, thanks!

@Tiggilyboo do you mind adding the CI changes, or would you prefer for me to push to your branch?

@Tiggilyboo
Copy link
Contributor Author

Sorry for the delay, I've added the build check in the workflow. Let me know if anything looks off

Copy link
Member

@WillLillis WillLillis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides the extra empty line, everything LGTM. Thanks!

@clason clason merged commit ee8d529 into tree-sitter:master Mar 25, 2025
12 checks passed
@tree-sitter-ci-bot
Copy link

Git push to origin failed for release-0.25 with exitcode 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tree sitter 0.25 does not compile with no_std ("std" feature disabled)

4 participants