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

Refuse to codegen an upstream static. #100211

Merged
merged 1 commit into from
Aug 15, 2022
Merged
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
5 changes: 5 additions & 0 deletions compiler/rustc_monomorphize/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,11 @@ fn should_codegen_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: &Instance<'tcx>) ->
return false;
}

if let DefKind::Static(_) = tcx.def_kind(def_id) {
// We cannot monomorphize statics from upstream crates.
return false;
}

if !tcx.is_mir_available(def_id) {
bug!("no MIR available for {:?}", def_id);
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/run-make/issue-85401-static-mir/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-include ../../run-make-fulldeps/tools.mk

# Regression test for issue #85401
# Verify that we do not ICE when trying to access MIR for statics,
# but emit an error when linking.

OUTPUT_FILE := $(TMPDIR)/build-output

all:
$(RUSTC) --crate-type rlib --crate-name foo -Crelocation-model=pic --edition=2018 foo.rs -Zalways-encode-mir=yes --emit metadata -o $(TMPDIR)/libfoo.rmeta
$(RUSTC) --crate-type rlib --crate-name bar -Crelocation-model=pic --edition=2018 bar.rs -o $(TMPDIR)/libbar.rlib --extern=foo=$(TMPDIR)/libfoo.rmeta
$(RUSTC) --crate-type bin --crate-name baz -Crelocation-model=pic --edition=2018 baz.rs -o $(TMPDIR)/baz -L $(TMPDIR) --extern=bar=$(TMPDIR)/libbar.rlib > $(OUTPUT_FILE) 2>&1; [ $$? -eq 1 ]
cat $(OUTPUT_FILE)
$(CGREP) 'crate `foo` required to be available in rlib format, but was not found in this form' < $(OUTPUT_FILE)
# -v tests are fragile, hopefully this text won't change
$(CGREP) -v "internal compiler error" < $(OUTPUT_FILE)
4 changes: 4 additions & 0 deletions src/test/run-make/issue-85401-static-mir/bar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub fn bar() {
println!("bar {}", foo::FOO);
foo::foo();
}
3 changes: 3 additions & 0 deletions src/test/run-make/issue-85401-static-mir/baz.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
bar::bar()
}
5 changes: 5 additions & 0 deletions src/test/run-make/issue-85401-static-mir/foo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub static FOO: &str = "foo";

pub fn foo() {
println!("foo");
}