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

check_expr_struct_fields: taint context with errors if struct definition is malformed #125947

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

let mut error_happened = false;

if variant.fields.len() != remaining_fields.len() {
// Some field is defined more than once. Make sure we don't try to
// instantiate this struct in static/const context.
let guar =
self.dcx().span_delayed_bug(expr.span, "struct fields have non-unique names");
self.set_tainted_by_errors(guar);
error_happened = true;
}

// Type-check each field.
for (idx, field) in hir_fields.iter().enumerate() {
let ident = tcx.adjust_ident(field.ident, variant.def_id);
Expand Down
4 changes: 2 additions & 2 deletions src/tools/run-make-support/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use std::process::{Command, Output};

use crate::is_windows;

use super::{bin_name, handle_failed_output};
use super::handle_failed_output;

fn run_common(name: &str) -> (Command, Output) {
let mut bin_path = PathBuf::new();
bin_path.push(env::var("TMPDIR").unwrap());
bin_path.push(&bin_name(name));
bin_path.push(name);
let ld_lib_path_envvar = env::var("LD_LIB_PATH_ENVVAR").unwrap();
let mut cmd = Command::new(bin_path);
cmd.env(&ld_lib_path_envvar, {
Expand Down
3 changes: 0 additions & 3 deletions src/tools/tidy/src/allowed_run_make_makefiles.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ run-make/allocator-shim-circular-deps/Makefile
run-make/allow-non-lint-warnings-cmdline/Makefile
run-make/archive-duplicate-names/Makefile
run-make/atomic-lock-free/Makefile
run-make/bare-outfile/Makefile
run-make/branch-protection-check-IBT/Makefile
run-make/c-dynamic-dylib/Makefile
run-make/c-dynamic-rlib/Makefile
Expand Down Expand Up @@ -40,7 +39,6 @@ run-make/emit-path-unhashed/Makefile
run-make/emit-shared-files/Makefile
run-make/emit-stack-sizes/Makefile
run-make/emit-to-stdout/Makefile
run-make/emit/Makefile
run-make/env-dep-info/Makefile
run-make/error-found-staticlib-instead-crate/Makefile
run-make/error-writing-dependencies/Makefile
Expand Down Expand Up @@ -147,7 +145,6 @@ run-make/min-global-align/Makefile
run-make/mingw-export-call-convention/Makefile
run-make/mismatching-target-triples/Makefile
run-make/missing-crate-dependency/Makefile
run-make/mixing-formats/Makefile
run-make/mixing-libs/Makefile
run-make/msvc-opt-minsize/Makefile
run-make/multiple-emits/Makefile
Expand Down
41 changes: 0 additions & 41 deletions tests/crashes/123917.rs

This file was deleted.

12 changes: 0 additions & 12 deletions tests/crashes/124552.rs

This file was deleted.

9 changes: 0 additions & 9 deletions tests/run-make/bare-outfile/Makefile

This file was deleted.

15 changes: 15 additions & 0 deletions tests/run-make/bare-outfile/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// This test checks that manually setting the output file as a bare file with no file extension
// still results in successful compilation.

//@ ignore-cross-compile

use run_make_support::{run, rustc, tmp_dir};
use std::env;
use std::fs;

fn main() {
fs::copy("foo.rs", tmp_dir().join("foo.rs")).unwrap();
env::set_current_dir(tmp_dir());
rustc().output("foo").input("foo.rs").run();
run("foo");
}
22 changes: 0 additions & 22 deletions tests/run-make/emit/Makefile

This file was deleted.

19 changes: 19 additions & 0 deletions tests/run-make/emit/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// A bug from 2015 would cause errors when emitting multiple types of files
// in the same rustc call. A fix was created in #30452. This test checks that rustc still compiles
// a source file successfully when emission of multiple output artifacts are requested.
// See https://github.com/rust-lang/rust/pull/30452

//@ ignore-cross-compile

use run_make_support::{run, rustc};

fn main() {
let opt_levels = ["0", "1", "2", "3", "s", "z"];
for level in opt_levels {
rustc().opt_level(level).emit("llvm-bc,llvm-ir,asm,obj,link").input("test-24876.rs").run();
}
for level in opt_levels {
rustc().opt_level(level).emit("llvm-bc,llvm-ir,asm,obj,link").input("test-26235.rs").run();
run("test-26235");
}
}
75 changes: 0 additions & 75 deletions tests/run-make/mixing-formats/Makefile

This file was deleted.

94 changes: 94 additions & 0 deletions tests/run-make/mixing-formats/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Testing various mixings of rlibs and dylibs. Makes sure that it's possible to
// link an rlib to a dylib. The dependency tree among the file looks like:
//
// foo
// / \
// bar1 bar2
// / \ /
// baz baz2
//
// This is generally testing the permutations of the foo/bar1/bar2 layer against
// the baz/baz2 layer

//@ ignore-cross-compile

use run_make_support::{rustc, tmp_dir};
use std::fs;

fn test_with_teardown(rustc_calls: impl Fn()) {
rustc_calls();
//FIXME(Oneirical): This should be replaced with the run-make-support fs wrappers.
fs::remove_dir_all(tmp_dir()).unwrap();
fs::create_dir(tmp_dir()).unwrap();
}

fn main() {
test_with_teardown(|| {
// Building just baz
rustc().crate_type("rlib").input("foo.rs").run();
rustc().crate_type("dylib").input("bar1.rs").arg("-Cprefer-dynamic").run();
rustc().crate_type("dylib,rlib").input("baz.rs").arg("-Cprefer-dynamic").run();
rustc().crate_type("bin").input("baz.rs").run();
});
test_with_teardown(|| {
rustc().crate_type("dylib").input("foo.rs").arg("-Cprefer-dynamic").run();
rustc().crate_type("rlib").input("bar1.rs").run();
rustc().crate_type("dylib,rlib").input("baz.rs").arg("-Cprefer-dynamic").run();
rustc().crate_type("bin").input("baz.rs").run();
});
test_with_teardown(|| {
// Building baz2
rustc().crate_type("rlib").input("foo.rs").run();
rustc().crate_type("dylib").input("bar1.rs").arg("-Cprefer-dynamic").run();
rustc().crate_type("dylib").input("bar2.rs").arg("-Cprefer-dynamic").run();
rustc().crate_type("dylib").input("baz2.rs").run_fail_assert_exit_code(1);
rustc().crate_type("bin").input("baz2.rs").run_fail_assert_exit_code(1);
});
test_with_teardown(|| {
rustc().crate_type("rlib").input("foo.rs").run();
rustc().crate_type("rlib").input("bar1.rs").run();
rustc().crate_type("dylib").input("bar2.rs").arg("-Cprefer-dynamic").run();
rustc().crate_type("dylib,rlib").input("baz2.rs").run();
rustc().crate_type("bin").input("baz2.rs").run();
});
test_with_teardown(|| {
rustc().crate_type("rlib").input("foo.rs").run();
rustc().crate_type("dylib").input("bar1.rs").arg("-Cprefer-dynamic").run();
rustc().crate_type("rlib").input("bar2.rs").run();
rustc().crate_type("dylib,rlib").input("baz2.rs").arg("-Cprefer-dynamic").run();
rustc().crate_type("bin").input("baz2.rs").run();
});
test_with_teardown(|| {
rustc().crate_type("rlib").input("foo.rs").run();
rustc().crate_type("rlib").input("bar1.rs").run();
rustc().crate_type("rlib").input("bar2.rs").run();
rustc().crate_type("dylib,rlib").input("baz2.rs").arg("-Cprefer-dynamic").run();
rustc().crate_type("bin").input("baz2.rs").run();
});
test_with_teardown(|| {
rustc().crate_type("dylib").input("foo.rs").arg("-Cprefer-dynamic").run();
rustc().crate_type("rlib").input("bar1.rs").run();
rustc().crate_type("rlib").input("bar2.rs").run();
rustc().crate_type("dylib,rlib").input("baz2.rs").arg("-Cprefer-dynamic").run();
rustc().crate_type("bin").input("baz2.rs").run();
});
test_with_teardown(|| {
rustc().crate_type("dylib").input("foo.rs").arg("-Cprefer-dynamic").run();
rustc().crate_type("dylib").input("bar1.rs").arg("-Cprefer-dynamic").run();
rustc().crate_type("rlib").input("bar2.rs").run();
rustc().crate_type("dylib,rlib").input("baz2.rs").run();
rustc().crate_type("bin").input("baz2.rs").run();
});
test_with_teardown(|| {
rustc().crate_type("dylib").input("foo.rs").arg("-Cprefer-dynamic").run();
rustc().crate_type("rlib").input("bar1.rs").run();
rustc().crate_type("dylib").input("bar2.rs").arg("-Cprefer-dynamic").run();
rustc().crate_type("dylib,rlib").input("baz2.rs").run();
rustc().crate_type("bin").input("baz2.rs").run();
});
rustc().crate_type("dylib").input("foo.rs").arg("-Cprefer-dynamic").run();
rustc().crate_type("dylib").input("bar1.rs").arg("-Cprefer-dynamic").run();
rustc().crate_type("dylib").input("bar2.rs").arg("-Cprefer-dynamic").run();
rustc().crate_type("dylib,rlib").input("baz2.rs").run();
rustc().crate_type("bin").input("baz2.rs").run();
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
//@ known-bug: rust-lang/rust #124464
// Don't const eval fields with ambiguous layout.
// See issues #125842 and #124464.

enum TestOption<T> {
TestSome(T),
TestSome(T),
//~^ ERROR the name `TestSome` is defined multiple times
}

pub struct Request {
bar: TestOption<u64>,
bar: u8,
//~^ ERROR field `bar` is already declared
}

fn default_instance() -> &'static Request {
Expand Down
22 changes: 22 additions & 0 deletions tests/ui/static/duplicated-fields-issue-124464.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error[E0428]: the name `TestSome` is defined multiple times
--> $DIR/duplicated-fields-issue-124464.rs:6:5
|
LL | TestSome(T),
| ----------- previous definition of the type `TestSome` here
LL | TestSome(T),
| ^^^^^^^^^^^ `TestSome` redefined here
|
= note: `TestSome` must be defined only once in the type namespace of this enum

error[E0124]: field `bar` is already declared
--> $DIR/duplicated-fields-issue-124464.rs:12:5
|
LL | bar: TestOption<u64>,
| -------------------- `bar` first declared here
LL | bar: u8,
| ^^^^^^^ field already declared

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0124, E0428.
For more information about an error, try `rustc --explain E0124`.
Loading