Skip to content

Commit

Permalink
saw-rustc: Skip non-functions in init_instances_from_tests without er…
Browse files Browse the repository at this point in the history
…roring

`crux-rustc` makes it an error to annotate non-functions with `#[crux::test]`
attributes. `saw-rustc` effectivelly annotates _everything_ with that
attribute, but we don't want to error when the annotation is applied to a
non-function, as typical Rust code will have more things (e.g., statics) than
just functions. We now make `saw-rustc` skip non-functions (thereby not marking
them as roots) instead of erroring.

Fixes #55.
  • Loading branch information
RyanGlScott committed Sep 9, 2023
1 parent e71792a commit 5ebd7a4
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions src/analyz/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,25 +790,21 @@ fn init_instances_from_tests(ms: &mut MirState, out: &mut impl JsonOutput) -> io
continue;
}

// Unless the DefId corresponds to a function, throw an error. We make
// an effort to cast a wide net in catching things that look like
// functions, as saw-rustc will try to initialize _all_ functions, not
// just the ones that users opt into, as is the case with crux-rustc.
match tcx.def_kind(def_id) {
// A `fn` declaration.
DefKind::Fn => (),
// Anonymous constants. See #47.
DefKind::AnonConst => (),
// A synthesized constructor for a tuple struct or tuple enum
// variant. See #53.
DefKind::Ctor(_, _) => (),
_ => {
if tcx.def_kind(def_id) != DefKind::Fn {
// If the DefId does not correspond to a function, then don't mark
// the function as a root. For crux-rustc, then also throw an error,
// as it doesn't make sense for a user to attach a #[crux::test]
// attribute on anything besides functions. For saw-rustc, however,
// it is fine to move on without emitting an error, as there are
// likely other functions elsewhere in the code that will instead
// be marked as roots. See #55.
if ms.export_style == ExportStyle::ExportCruxTests {
tcx.sess.span_err(
tcx.def_span(def_id),
"#[test] can only be applied to functions",
);
continue;
},
}
continue;
}
if tcx.generics_of(def_id).count() > 0 {
// If we are using crux-rustc, then attempting to mark a generic
Expand Down

0 comments on commit 5ebd7a4

Please sign in to comment.