Skip to content

Commit 11fcf9c

Browse files
committedDec 14, 2023
Auto merge of #118942 - GuillaumeGomez:rollup-n5vtatg, r=GuillaumeGomez
Rollup of 4 pull requests Successful merges: - #118770 (Fix cases where std accidentally relied on inline(never)) - #118910 ([rustdoc] Use Map instead of Object for source files and search index) - #118914 (Unconditionally register alias-relate in projection goal) - #118935 (interpret: extend comment on the inhabitedness check in downcast) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 2ecba0f + a15f60d commit 11fcf9c

File tree

13 files changed

+203
-117
lines changed

13 files changed

+203
-117
lines changed
 

‎compiler/rustc_const_eval/src/interpret/projection.rs

+18
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,24 @@ where
208208
if layout.abi.is_uninhabited() {
209209
// `read_discriminant` should have excluded uninhabited variants... but ConstProp calls
210210
// us on dead code.
211+
// In the future we might want to allow this to permit code like this:
212+
// (this is a Rust/MIR pseudocode mix)
213+
// ```
214+
// enum Option2 {
215+
// Some(i32, !),
216+
// None,
217+
// }
218+
//
219+
// fn panic() -> ! { panic!() }
220+
//
221+
// let x: Option2;
222+
// x.Some.0 = 42;
223+
// x.Some.1 = panic();
224+
// SetDiscriminant(x, Some);
225+
// ```
226+
// However, for now we don't generate such MIR, and this check here *has* found real
227+
// bugs (see https://github.com/rust-lang/rust/issues/115145), so we will keep rejecting
228+
// it.
211229
throw_inval!(ConstPropNonsense)
212230
}
213231
// This cannot be `transmute` as variants *can* have a smaller size than the entire enum.

‎compiler/rustc_mir_transform/src/cross_crate_inline.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
2222
return false;
2323
}
2424

25+
// This just reproduces the logic from Instance::requires_inline.
26+
match tcx.def_kind(def_id) {
27+
DefKind::Ctor(..) | DefKind::Closure => return true,
28+
DefKind::Fn | DefKind::AssocFn => {}
29+
_ => return false,
30+
}
31+
32+
// From this point on, it is valid to return true or false.
33+
if tcx.sess.opts.unstable_opts.cross_crate_inline_threshold == InliningThreshold::Always {
34+
return true;
35+
}
36+
2537
// Obey source annotations first; this is important because it means we can use
2638
// #[inline(never)] to force code generation.
2739
match codegen_fn_attrs.inline {
@@ -30,13 +42,6 @@ fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
3042
_ => {}
3143
}
3244

33-
// This just reproduces the logic from Instance::requires_inline.
34-
match tcx.def_kind(def_id) {
35-
DefKind::Ctor(..) | DefKind::Closure => return true,
36-
DefKind::Fn | DefKind::AssocFn => {}
37-
_ => return false,
38-
}
39-
4045
// Don't do any inference when incremental compilation is enabled; the additional inlining that
4146
// inference permits also creates more work for small edits.
4247
if tcx.sess.opts.incremental.is_some() {

‎compiler/rustc_monomorphize/src/collector.rs

+16
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ use rustc_middle::mir::visit::Visitor as MirVisitor;
176176
use rustc_middle::mir::{self, Location};
177177
use rustc_middle::query::TyCtxtAt;
178178
use rustc_middle::ty::adjustment::{CustomCoerceUnsized, PointerCoercion};
179+
use rustc_middle::ty::layout::ValidityRequirement;
179180
use rustc_middle::ty::print::with_no_trimmed_paths;
180181
use rustc_middle::ty::{
181182
self, AssocKind, GenericParamDefKind, Instance, InstanceDef, Ty, TyCtxt, TypeFoldable,
@@ -923,6 +924,21 @@ fn visit_instance_use<'tcx>(
923924
return;
924925
}
925926

927+
// The intrinsics assert_inhabited, assert_zero_valid, and assert_mem_uninitialized_valid will
928+
// be lowered in codegen to nothing or a call to panic_nounwind. So if we encounter any
929+
// of those intrinsics, we need to include a mono item for panic_nounwind, else we may try to
930+
// codegen a call to that function without generating code for the function itself.
931+
if let ty::InstanceDef::Intrinsic(def_id) = instance.def {
932+
let name = tcx.item_name(def_id);
933+
if let Some(_requirement) = ValidityRequirement::from_intrinsic(name) {
934+
let def_id = tcx.lang_items().get(LangItem::PanicNounwind).unwrap();
935+
let panic_instance = Instance::mono(tcx, def_id);
936+
if should_codegen_locally(tcx, &panic_instance) {
937+
output.push(create_fn_mono_item(tcx, panic_instance, source));
938+
}
939+
}
940+
}
941+
926942
match instance.def {
927943
ty::InstanceDef::Virtual(..) | ty::InstanceDef::Intrinsic(_) => {
928944
if !is_direct_call {

‎compiler/rustc_monomorphize/src/partitioning.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,17 @@ where
212212
let cgu_name_cache = &mut FxHashMap::default();
213213

214214
for mono_item in mono_items {
215-
// Handle only root items directly here. Inlined items are handled at
216-
// the bottom of the loop based on reachability.
215+
// Handle only root (GloballyShared) items directly here. Inlined (LocalCopy) items
216+
// are handled at the bottom of the loop based on reachability, with one exception.
217+
// The #[lang = "start"] item is the program entrypoint, so there are no calls to it in MIR.
218+
// So even if its mode is LocalCopy, we need to treat it like a root.
217219
match mono_item.instantiation_mode(cx.tcx) {
218220
InstantiationMode::GloballyShared { .. } => {}
219-
InstantiationMode::LocalCopy => continue,
221+
InstantiationMode::LocalCopy => {
222+
if Some(mono_item.def_id()) != cx.tcx.lang_items().start_fn() {
223+
continue;
224+
}
225+
}
220226
}
221227

222228
let characteristic_def_id = characteristic_def_id_of_mono_item(cx.tcx, mono_item);

‎compiler/rustc_trait_selection/src/solve/project_goals.rs

+23-11
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,28 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
88
&mut self,
99
goal: Goal<'tcx, ProjectionPredicate<'tcx>>,
1010
) -> QueryResult<'tcx> {
11-
match goal.predicate.term.unpack() {
12-
ty::TermKind::Ty(term) => {
13-
let alias = goal.predicate.projection_ty.to_ty(self.tcx());
14-
self.eq(goal.param_env, alias, term)?;
15-
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
16-
}
17-
// FIXME(associated_const_equality): actually do something here.
18-
ty::TermKind::Const(_) => {
19-
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
20-
}
21-
}
11+
let tcx = self.tcx();
12+
let projection_term = match goal.predicate.term.unpack() {
13+
ty::TermKind::Ty(_) => goal.predicate.projection_ty.to_ty(tcx).into(),
14+
ty::TermKind::Const(_) => ty::Const::new_unevaluated(
15+
tcx,
16+
ty::UnevaluatedConst::new(
17+
goal.predicate.projection_ty.def_id,
18+
goal.predicate.projection_ty.args,
19+
),
20+
tcx.type_of(goal.predicate.projection_ty.def_id)
21+
.instantiate(tcx, goal.predicate.projection_ty.args),
22+
)
23+
.into(),
24+
};
25+
self.add_goal(goal.with(
26+
tcx,
27+
ty::PredicateKind::AliasRelate(
28+
projection_term,
29+
goal.predicate.term,
30+
ty::AliasRelationDirection::Equate,
31+
),
32+
));
33+
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
2234
}
2335
}

‎library/std/src/rt.rs

-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ fn lang_start_internal(
155155
}
156156

157157
#[cfg(not(test))]
158-
#[inline(never)]
159158
#[lang = "start"]
160159
fn lang_start<T: crate::process::Termination + 'static>(
161160
main: fn() -> T,

‎src/librustdoc/html/render/search_index.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ pub(crate) fn build_index<'tcx>(
488488

489489
// Collect the index into a string
490490
format!(
491-
r#""{}":{}"#,
491+
r#"["{}",{}]"#,
492492
krate.name(tcx),
493493
serde_json::to_string(&CrateData {
494494
doc: crate_doc,

‎src/librustdoc/html/render/write_shared.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -167,23 +167,24 @@ pub(super) fn write_shared(
167167
let mut krates = Vec::new();
168168

169169
if path.exists() {
170-
let prefix = format!("\"{krate}\"");
170+
let prefix = format!("[\"{krate}\"");
171171
for line in BufReader::new(File::open(path)?).lines() {
172172
let line = line?;
173-
if !line.starts_with('"') {
173+
if !line.starts_with("[\"") {
174174
continue;
175175
}
176176
if line.starts_with(&prefix) {
177177
continue;
178178
}
179-
if line.ends_with(",\\") {
179+
if line.ends_with("],\\") {
180180
ret.push(line[..line.len() - 2].to_string());
181181
} else {
182182
// Ends with "\\" (it's the case for the last added crate line)
183183
ret.push(line[..line.len() - 1].to_string());
184184
}
185185
krates.push(
186-
line.split('"')
186+
line[1..] // We skip the `[` parent at the beginning of the line.
187+
.split('"')
187188
.find(|s| !s.is_empty())
188189
.map(|s| s.to_owned())
189190
.unwrap_or_else(String::new),
@@ -285,7 +286,7 @@ pub(super) fn write_shared(
285286
let (mut all_sources, _krates) =
286287
try_err!(collect_json(&dst, krate.name(cx.tcx()).as_str()), &dst);
287288
all_sources.push(format!(
288-
r#""{}":{}"#,
289+
r#"["{}",{}]"#,
289290
&krate.name(cx.tcx()),
290291
hierarchy
291292
.to_json_string()
@@ -296,9 +297,9 @@ pub(super) fn write_shared(
296297
.replace("\\\"", "\\\\\"")
297298
));
298299
all_sources.sort();
299-
let mut v = String::from("var srcIndex = JSON.parse('{\\\n");
300+
let mut v = String::from("const srcIndex = new Map(JSON.parse('[\\\n");
300301
v.push_str(&all_sources.join(",\\\n"));
301-
v.push_str("\\\n}');\ncreateSrcSidebar();\n");
302+
v.push_str("\\\n]'));\ncreateSrcSidebar();\n");
302303
Ok(v.into_bytes())
303304
};
304305
write_invocation_specific("src-files.js", &make_sources)?;
@@ -316,11 +317,11 @@ pub(super) fn write_shared(
316317
// with rustdoc running in parallel.
317318
all_indexes.sort();
318319
write_invocation_specific("search-index.js", &|| {
319-
let mut v = String::from("var searchIndex = JSON.parse('{\\\n");
320+
let mut v = String::from("const searchIndex = new Map(JSON.parse('[\\\n");
320321
v.push_str(&all_indexes.join(",\\\n"));
321322
v.push_str(
322323
r#"\
323-
}');
324+
]'));
324325
if (typeof window !== 'undefined' && window.initSearch) {window.initSearch(searchIndex)};
325326
if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex};
326327
"#,

0 commit comments

Comments
 (0)