Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected saw-rustc compilation failure (#[test] can only be applied to functions) #47

Closed
RyanGlScott opened this issue Jun 29, 2023 · 2 comments · Fixed by #49
Closed
Assignees
Labels

Comments

@RyanGlScott
Copy link
Contributor

RyanGlScott commented Jun 29, 2023

saw-rustc fails to compile this rather simple Rust program:

pub fn f() -> i32 {
    let xs : [i32; 3] = [0; 3];
    xs[0]
}
$ SAW_RUST_LIBRARY_PATH=~/Documents/Hacking/Haskell/crucible/crux-mir/rlibs saw-rustc test.rs 
test build - extract output path - ["rustc", "test.rs", "--test", "--target", "x86_64-unknown-linux-gnu", "--cfg", "crux", "-L", "/home/rscott/Documents/Hacking/Haskell/crucible/crux-mir/rlibs"]
test build - ["rustc", "test.rs", "--target", "x86_64-unknown-linux-gnu", "--cfg", "crux", "-L", "/home/rscott/Documents/Hacking/Haskell/crucible/crux-mir/rlibs", "--cfg", "crux_top_level", "--crate-type", "rlib"]
error: #[test] can only be applied to functions
 --> test.rs:2:29
  |
2 |     let xs : [i32; 3] = [0; 3];
  |                             ^

error: #[test] can only be applied to functions
 --> test.rs:2:20
  |
2 |     let xs : [i32; 3] = [0; 3];
  |                    ^

note: Emitting MIR for test/3a1fbbbh::f[0]

error: aborting due to 2 previous errors

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: ErrorReported', src/bin/mir-json-rustc-wrapper.rs:255:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I'm not entirely sure what is going on here, but my hunch is that saw-rustc is inserting #[test] attributes where it shouldn't.

@RyanGlScott RyanGlScott self-assigned this Jun 29, 2023
@spernsteiner
Copy link
Collaborator

If saw-rustc is trying to inject #[test] attributes on functions, it might be adding them on AnonConsts as well, since those have DefIds and MIR bodies even though they aren't actual functions.

@RyanGlScott
Copy link
Contributor Author

Well-spotted! Indeed, we have this code:

mir-json/src/analyz/mod.rs

Lines 706 to 712 in 3a621e1

if tcx.def_kind(def_id) != Some(DefKind::Fn) {
tcx.sess.span_err(
tcx.def_span(def_id),
"#[test] can only be applied to functions",
);
continue;
}

Which only checks for full-blooded functions. Changing this to the following (on the rustup-2023-01-23 branch):

diff --git a/src/analyz/mod.rs b/src/analyz/mod.rs
index 6bb1b87..22d869e 100644
--- a/src/analyz/mod.rs
+++ b/src/analyz/mod.rs
@@ -808,7 +808,7 @@ fn init_instances_from_tests(ms: &mut MirState, out: &mut impl JsonOutput) -> io
             continue;
         }
 
-        if tcx.def_kind(def_id) != DefKind::Fn {
+        if !([DefKind::Fn, DefKind::AnonConst].contains(&tcx.def_kind(def_id))) {
             tcx.sess.span_err(
                 tcx.def_span(def_id),
                 "#[test] can only be applied to functions",

Fixes the issue. Unfortunately, AnonConst doesn't exist in the 2020 Rust nightly that the master branch uses, so it's unclear to me how to backport this fix. But in any case, I'll fix this issue as part of my upcoming PR to upgrade to a 2023 nightly.

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 a pull request may close this issue.

2 participants