Skip to content

Commit d6b6503

Browse files
authoredAug 15, 2022
Rollup merge of #100211 - cjgillot:ctfe-mir-available, r=michaelwoerister
Refuse to codegen an upstream static. Fixes #85401
2 parents 80ed61f + d3fee8d commit d6b6503

File tree

5 files changed

+33
-0
lines changed

5 files changed

+33
-0
lines changed
 

‎compiler/rustc_monomorphize/src/collector.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,11 @@ fn should_codegen_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: &Instance<'tcx>) ->
10271027
return false;
10281028
}
10291029

1030+
if let DefKind::Static(_) = tcx.def_kind(def_id) {
1031+
// We cannot monomorphize statics from upstream crates.
1032+
return false;
1033+
}
1034+
10301035
if !tcx.is_mir_available(def_id) {
10311036
bug!("no MIR available for {:?}", def_id);
10321037
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-include ../../run-make-fulldeps/tools.mk
2+
3+
# Regression test for issue #85401
4+
# Verify that we do not ICE when trying to access MIR for statics,
5+
# but emit an error when linking.
6+
7+
OUTPUT_FILE := $(TMPDIR)/build-output
8+
9+
all:
10+
$(RUSTC) --crate-type rlib --crate-name foo -Crelocation-model=pic --edition=2018 foo.rs -Zalways-encode-mir=yes --emit metadata -o $(TMPDIR)/libfoo.rmeta
11+
$(RUSTC) --crate-type rlib --crate-name bar -Crelocation-model=pic --edition=2018 bar.rs -o $(TMPDIR)/libbar.rlib --extern=foo=$(TMPDIR)/libfoo.rmeta
12+
$(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 ]
13+
cat $(OUTPUT_FILE)
14+
$(CGREP) 'crate `foo` required to be available in rlib format, but was not found in this form' < $(OUTPUT_FILE)
15+
# -v tests are fragile, hopefully this text won't change
16+
$(CGREP) -v "internal compiler error" < $(OUTPUT_FILE)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub fn bar() {
2+
println!("bar {}", foo::FOO);
3+
foo::foo();
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
bar::bar()
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub static FOO: &str = "foo";
2+
3+
pub fn foo() {
4+
println!("foo");
5+
}

0 commit comments

Comments
 (0)
Please sign in to comment.