From 63ffdfdd1776afa2e82bbd3d2ff8ff7b7f0d5b67 Mon Sep 17 00:00:00 2001 From: yvt Date: Mon, 25 Apr 2022 21:19:42 +0900 Subject: Add compilation tests with optimization enabled Introduces a new variant of `tests/lib.rs` that compiles the source files in `tests/run` with `-Copt-level=3`. --- tests/lang_tests_common.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++ tests/lang_tests_debug.rs | 5 ++++ tests/lang_tests_release.rs | 5 ++++ tests/lib.rs | 50 --------------------------------- tests/run/int_overflow.rs | 17 ++++++++++-- 5 files changed, 92 insertions(+), 53 deletions(-) create mode 100644 tests/lang_tests_common.rs create mode 100644 tests/lang_tests_debug.rs create mode 100644 tests/lang_tests_release.rs delete mode 100644 tests/lib.rs (limited to 'tests') diff --git a/tests/lang_tests_common.rs b/tests/lang_tests_common.rs new file mode 100644 index 00000000000..8e378177e24 --- /dev/null +++ b/tests/lang_tests_common.rs @@ -0,0 +1,68 @@ +//! The common code for `tests/lang_tests_*.rs` +use std::{ + env::{self, current_dir}, + path::PathBuf, + process::Command, +}; + +use lang_tester::LangTester; +use tempfile::TempDir; + +/// Controls the compile options (e.g., optimization level) used to compile +/// test code. +#[allow(dead_code)] // Each test crate picks one variant +pub enum Profile { + Debug, + Release, +} + +pub fn main_inner(profile: Profile) { + let tempdir = TempDir::new().expect("temp dir"); + let current_dir = current_dir().expect("current dir"); + let current_dir = current_dir.to_str().expect("current dir").to_string(); + let gcc_path = include_str!("../gcc_path"); + let gcc_path = gcc_path.trim(); + env::set_var("LD_LIBRARY_PATH", gcc_path); + LangTester::new() + .test_dir("tests/run") + .test_file_filter(|path| path.extension().expect("extension").to_str().expect("to_str") == "rs") + .test_extract(|source| { + let lines = + source.lines() + .skip_while(|l| !l.starts_with("//")) + .take_while(|l| l.starts_with("//")) + .map(|l| &l[2..]) + .collect::>() + .join("\n"); + Some(lines) + }) + .test_cmds(move |path| { + // Test command 1: Compile `x.rs` into `tempdir/x`. + let mut exe = PathBuf::new(); + exe.push(&tempdir); + exe.push(path.file_stem().expect("file_stem")); + let mut compiler = Command::new("rustc"); + compiler.args(&[ + &format!("-Zcodegen-backend={}/target/debug/librustc_codegen_gcc.so", current_dir), + "--sysroot", &format!("{}/build_sysroot/sysroot/", current_dir), + "-Zno-parallel-llvm", + "-C", "panic=abort", + "-C", "link-arg=-lc", + "-o", exe.to_str().expect("to_str"), + path.to_str().expect("to_str"), + ]); + match profile { + Profile::Debug => {} + Profile::Release => { + compiler.args(&[ + "-C", "opt-level=3", + "-C", "lto=no", + ]); + } + } + // Test command 2: run `tempdir/x`. + let runtime = Command::new(exe); + vec![("Compiler", compiler), ("Run-time", runtime)] + }) + .run(); +} diff --git a/tests/lang_tests_debug.rs b/tests/lang_tests_debug.rs new file mode 100644 index 00000000000..96bd74883ff --- /dev/null +++ b/tests/lang_tests_debug.rs @@ -0,0 +1,5 @@ +mod lang_tests_common; + +fn main() { + lang_tests_common::main_inner(lang_tests_common::Profile::Debug); +} diff --git a/tests/lang_tests_release.rs b/tests/lang_tests_release.rs new file mode 100644 index 00000000000..35d5d60c33e --- /dev/null +++ b/tests/lang_tests_release.rs @@ -0,0 +1,5 @@ +mod lang_tests_common; + +fn main() { + lang_tests_common::main_inner(lang_tests_common::Profile::Release); +} diff --git a/tests/lib.rs b/tests/lib.rs deleted file mode 100644 index 8ee35b30bc8..00000000000 --- a/tests/lib.rs +++ /dev/null @@ -1,50 +0,0 @@ -use std::{ - env::{self, current_dir}, - path::PathBuf, - process::Command, -}; - -use lang_tester::LangTester; -use tempfile::TempDir; - -fn main() { - let tempdir = TempDir::new().expect("temp dir"); - let current_dir = current_dir().expect("current dir"); - let current_dir = current_dir.to_str().expect("current dir").to_string(); - let gcc_path = include_str!("../gcc_path"); - let gcc_path = gcc_path.trim(); - env::set_var("LD_LIBRARY_PATH", gcc_path); - LangTester::new() - .test_dir("tests/run") - .test_file_filter(|path| path.extension().expect("extension").to_str().expect("to_str") == "rs") - .test_extract(|source| { - let lines = - source.lines() - .skip_while(|l| !l.starts_with("//")) - .take_while(|l| l.starts_with("//")) - .map(|l| &l[2..]) - .collect::>() - .join("\n"); - Some(lines) - }) - .test_cmds(move |path| { - // Test command 1: Compile `x.rs` into `tempdir/x`. - let mut exe = PathBuf::new(); - exe.push(&tempdir); - exe.push(path.file_stem().expect("file_stem")); - let mut compiler = Command::new("rustc"); - compiler.args(&[ - &format!("-Zcodegen-backend={}/target/debug/librustc_codegen_gcc.so", current_dir), - "--sysroot", &format!("{}/build_sysroot/sysroot/", current_dir), - "-Zno-parallel-llvm", - "-C", "panic=abort", - "-C", "link-arg=-lc", - "-o", exe.to_str().expect("to_str"), - path.to_str().expect("to_str"), - ]); - // Test command 2: run `tempdir/x`. - let runtime = Command::new(exe); - vec![("Compiler", compiler), ("Run-time", runtime)] - }) - .run(); -} diff --git a/tests/run/int_overflow.rs b/tests/run/int_overflow.rs index 6477b839828..ea2c5add962 100644 --- a/tests/run/int_overflow.rs +++ b/tests/run/int_overflow.rs @@ -1,7 +1,7 @@ // Compiler: // // Run-time: -// stdout: Panicking +// stdout: Success // status: signal #![allow(unused_attributes)] @@ -64,7 +64,9 @@ mod intrinsics { #[no_mangle] pub fn panic(_msg: &str) -> ! { unsafe { - libc::puts("Panicking\0" as *const str as *const u8); + // Panicking is expected iff overflow checking is enabled. + #[cfg(debug_assertions)] + libc::puts("Success\0" as *const str as *const u8); libc::fflush(libc::stdout); intrinsics::abort(); } @@ -124,6 +126,15 @@ impl Add for isize { #[start] fn main(mut argc: isize, _argv: *const *const u8) -> isize { let int = 9223372036854775807isize; - let int = int + argc; + let int = int + argc; // overflow + + // If overflow checking is disabled, we should reach here. + #[cfg(not(debug_assertions))] + unsafe { + libc::puts("Success\0" as *const str as *const u8); + libc::fflush(libc::stdout); + intrinsics::abort(); + } + int } -- cgit 1.4.1-3-g733a5