Skip to content

Commit 1efb705

Browse files
committed
Auto merge of rust-lang#136169 - Zalathar:rollup-w09vw0j, r=Zalathar
Rollup of 7 pull requests Successful merges: - rust-lang#133151 (Trim extra whitespace in fn ptr suggestion span) - rust-lang#134290 (Windows x86: Change i128 to return via the vector ABI) - rust-lang#135886 (Document purpose of closure in from_fn.rs more clearly) - rust-lang#136012 (Document powf and powi values that are always 1.0) - rust-lang#136104 (Add mermaid graphs of NLL regions and SCCs to polonius MIR dump) - rust-lang#136143 (Update books) - rust-lang#136153 (Locate asan-odr-win with other sanitizer tests) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 2f348cb + f8e9464 commit 1efb705

File tree

22 files changed

+394
-126
lines changed

22 files changed

+394
-126
lines changed

compiler/rustc_borrowck/src/polonius/dump.rs

+136-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
use std::io;
22

3+
use rustc_data_structures::fx::FxHashSet;
4+
use rustc_index::IndexVec;
35
use rustc_middle::mir::pretty::{
46
PassWhere, PrettyPrintMirOptions, create_dump_file, dump_enabled, dump_mir_to_writer,
57
};
68
use rustc_middle::mir::{Body, ClosureRegionRequirements};
7-
use rustc_middle::ty::TyCtxt;
9+
use rustc_middle::ty::{RegionVid, TyCtxt};
810
use rustc_session::config::MirIncludeSpans;
911

1012
use crate::borrow_set::BorrowSet;
13+
use crate::constraints::OutlivesConstraint;
1114
use crate::polonius::{LocalizedOutlivesConstraint, LocalizedOutlivesConstraintSet};
15+
use crate::type_check::Locations;
1216
use crate::{BorrowckInferCtxt, RegionInferenceContext};
1317

1418
/// `-Zdump-mir=polonius` dumps MIR annotated with NLL and polonius specific information.
@@ -50,6 +54,8 @@ pub(crate) fn dump_polonius_mir<'tcx>(
5054
/// - the NLL MIR
5155
/// - the list of polonius localized constraints
5256
/// - a mermaid graph of the CFG
57+
/// - a mermaid graph of the NLL regions and the constraints between them
58+
/// - a mermaid graph of the NLL SCCs and the constraints between them
5359
fn emit_polonius_dump<'tcx>(
5460
tcx: TyCtxt<'tcx>,
5561
body: &Body<'tcx>,
@@ -68,7 +74,7 @@ fn emit_polonius_dump<'tcx>(
6874
// Section 1: the NLL + Polonius MIR.
6975
writeln!(out, "<div>")?;
7076
writeln!(out, "Raw MIR dump")?;
71-
writeln!(out, "<code><pre>")?;
77+
writeln!(out, "<pre><code>")?;
7278
emit_html_mir(
7379
tcx,
7480
body,
@@ -78,15 +84,31 @@ fn emit_polonius_dump<'tcx>(
7884
closure_region_requirements,
7985
out,
8086
)?;
81-
writeln!(out, "</pre></code>")?;
87+
writeln!(out, "</code></pre>")?;
8288
writeln!(out, "</div>")?;
8389

8490
// Section 2: mermaid visualization of the CFG.
8591
writeln!(out, "<div>")?;
8692
writeln!(out, "Control-flow graph")?;
87-
writeln!(out, "<code><pre class='mermaid'>")?;
93+
writeln!(out, "<pre class='mermaid'>")?;
8894
emit_mermaid_cfg(body, out)?;
89-
writeln!(out, "</pre></code>")?;
95+
writeln!(out, "</pre>")?;
96+
writeln!(out, "</div>")?;
97+
98+
// Section 3: mermaid visualization of the NLL region graph.
99+
writeln!(out, "<div>")?;
100+
writeln!(out, "NLL regions")?;
101+
writeln!(out, "<pre class='mermaid'>")?;
102+
emit_mermaid_nll_regions(regioncx, out)?;
103+
writeln!(out, "</pre>")?;
104+
writeln!(out, "</div>")?;
105+
106+
// Section 4: mermaid visualization of the NLL SCC graph.
107+
writeln!(out, "<div>")?;
108+
writeln!(out, "NLL SCCs")?;
109+
writeln!(out, "<pre class='mermaid'>")?;
110+
emit_mermaid_nll_sccs(regioncx, out)?;
111+
writeln!(out, "</pre>")?;
90112
writeln!(out, "</div>")?;
91113

92114
// Finalize the dump with the HTML epilogue.
@@ -261,3 +283,112 @@ fn emit_mermaid_cfg(body: &Body<'_>, out: &mut dyn io::Write) -> io::Result<()>
261283

262284
Ok(())
263285
}
286+
287+
/// Emits a region's label: index, universe, external name.
288+
fn render_region(
289+
region: RegionVid,
290+
regioncx: &RegionInferenceContext<'_>,
291+
out: &mut dyn io::Write,
292+
) -> io::Result<()> {
293+
let def = regioncx.region_definition(region);
294+
let universe = def.universe;
295+
296+
write!(out, "'{}", region.as_usize())?;
297+
if !universe.is_root() {
298+
write!(out, "/{universe:?}")?;
299+
}
300+
if let Some(name) = def.external_name.and_then(|e| e.get_name()) {
301+
write!(out, " ({name})")?;
302+
}
303+
Ok(())
304+
}
305+
306+
/// Emits a mermaid flowchart of the NLL regions and the outlives constraints between them, similar
307+
/// to the graphviz version.
308+
fn emit_mermaid_nll_regions<'tcx>(
309+
regioncx: &RegionInferenceContext<'tcx>,
310+
out: &mut dyn io::Write,
311+
) -> io::Result<()> {
312+
// The mermaid chart type: a top-down flowchart.
313+
writeln!(out, "flowchart TD")?;
314+
315+
// Emit the region nodes.
316+
for region in regioncx.var_infos.indices() {
317+
write!(out, "{}[\"", region.as_usize())?;
318+
render_region(region, regioncx, out)?;
319+
writeln!(out, "\"]")?;
320+
}
321+
322+
// Get a set of edges to check for the reverse edge being present.
323+
let edges: FxHashSet<_> = regioncx.outlives_constraints().map(|c| (c.sup, c.sub)).collect();
324+
325+
// Order (and deduplicate) edges for traversal, to display them in a generally increasing order.
326+
let constraint_key = |c: &OutlivesConstraint<'_>| {
327+
let min = c.sup.min(c.sub);
328+
let max = c.sup.max(c.sub);
329+
(min, max)
330+
};
331+
let mut ordered_edges: Vec<_> = regioncx.outlives_constraints().collect();
332+
ordered_edges.sort_by_key(|c| constraint_key(c));
333+
ordered_edges.dedup_by_key(|c| constraint_key(c));
334+
335+
for outlives in ordered_edges {
336+
// Source node.
337+
write!(out, "{} ", outlives.sup.as_usize())?;
338+
339+
// The kind of arrow: bidirectional if the opposite edge exists in the set.
340+
if edges.contains(&(outlives.sub, outlives.sup)) {
341+
write!(out, "&lt;")?;
342+
}
343+
write!(out, "-- ")?;
344+
345+
// Edge label from its `Locations`.
346+
match outlives.locations {
347+
Locations::All(_) => write!(out, "All")?,
348+
Locations::Single(location) => write!(out, "{:?}", location)?,
349+
}
350+
351+
// Target node.
352+
writeln!(out, " --> {}", outlives.sub.as_usize())?;
353+
}
354+
Ok(())
355+
}
356+
357+
/// Emits a mermaid flowchart of the NLL SCCs and the outlives constraints between them, similar
358+
/// to the graphviz version.
359+
fn emit_mermaid_nll_sccs<'tcx>(
360+
regioncx: &RegionInferenceContext<'tcx>,
361+
out: &mut dyn io::Write,
362+
) -> io::Result<()> {
363+
// The mermaid chart type: a top-down flowchart.
364+
writeln!(out, "flowchart TD")?;
365+
366+
// Gather and emit the SCC nodes.
367+
let mut nodes_per_scc: IndexVec<_, _> =
368+
regioncx.constraint_sccs().all_sccs().map(|_| Vec::new()).collect();
369+
for region in regioncx.var_infos.indices() {
370+
let scc = regioncx.constraint_sccs().scc(region);
371+
nodes_per_scc[scc].push(region);
372+
}
373+
for (scc, regions) in nodes_per_scc.iter_enumerated() {
374+
// The node label: the regions contained in the SCC.
375+
write!(out, "{scc}[\"SCC({scc}) = {{", scc = scc.as_usize())?;
376+
for (idx, &region) in regions.iter().enumerate() {
377+
render_region(region, regioncx, out)?;
378+
if idx < regions.len() - 1 {
379+
write!(out, ",")?;
380+
}
381+
}
382+
writeln!(out, "}}\"]")?;
383+
}
384+
385+
// Emit the edges between SCCs.
386+
let edges = regioncx.constraint_sccs().all_sccs().flat_map(|source| {
387+
regioncx.constraint_sccs().successors(source).iter().map(move |&target| (source, target))
388+
});
389+
for (source, target) in edges {
390+
writeln!(out, "{} --> {}", source.as_usize(), target.as_usize())?;
391+
}
392+
393+
Ok(())
394+
}

compiler/rustc_codegen_cranelift/src/abi/mod.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
126126
&mut self,
127127
name: &str,
128128
params: Vec<AbiParam>,
129-
returns: Vec<AbiParam>,
129+
mut returns: Vec<AbiParam>,
130130
args: &[Value],
131131
) -> Cow<'_, [Value]> {
132132
// Pass i128 arguments by-ref on Windows.
@@ -150,15 +150,19 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
150150
(params, args.into())
151151
};
152152

153-
// Return i128 using a return area pointer on Windows and s390x.
154-
let adjust_ret_param =
155-
if self.tcx.sess.target.is_like_windows || self.tcx.sess.target.arch == "s390x" {
156-
returns.len() == 1 && returns[0].value_type == types::I128
157-
} else {
158-
false
159-
};
153+
let ret_single_i128 = returns.len() == 1 && returns[0].value_type == types::I128;
154+
if ret_single_i128 && self.tcx.sess.target.is_like_windows {
155+
// Return i128 using the vector ABI on Windows
156+
returns[0].value_type = types::I64X2;
157+
158+
let ret = self.lib_call_unadjusted(name, params, returns, &args)[0];
160159

161-
if adjust_ret_param {
160+
// FIXME(bytecodealliance/wasmtime#6104) use bitcast instead of store to get from i64x2 to i128
161+
let ret_ptr = self.create_stack_slot(16, 16);
162+
ret_ptr.store(self, ret, MemFlags::trusted());
163+
Cow::Owned(vec![ret_ptr.load(self, types::I128, MemFlags::trusted())])
164+
} else if ret_single_i128 && self.tcx.sess.target.arch == "s390x" {
165+
// Return i128 using a return area pointer on s390x.
162166
let mut params = params;
163167
let mut args = args.to_vec();
164168

compiler/rustc_codegen_cranelift/src/cast.rs

+3-19
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,9 @@ pub(crate) fn clif_int_or_float_cast(
9696
},
9797
);
9898

99-
if fx.tcx.sess.target.is_like_windows {
100-
let ret = fx.lib_call(
101-
&name,
102-
vec![AbiParam::new(from_ty)],
103-
vec![AbiParam::new(types::I64X2)],
104-
&[from],
105-
)[0];
106-
// FIXME(bytecodealliance/wasmtime#6104) use bitcast instead of store to get from i64x2 to i128
107-
let ret_ptr = fx.create_stack_slot(16, 16);
108-
ret_ptr.store(fx, ret, MemFlags::trusted());
109-
ret_ptr.load(fx, types::I128, MemFlags::trusted())
110-
} else {
111-
fx.lib_call(
112-
&name,
113-
vec![AbiParam::new(from_ty)],
114-
vec![AbiParam::new(types::I128)],
115-
&[from],
116-
)[0]
117-
}
99+
fx.lib_call(&name, vec![AbiParam::new(from_ty)], vec![AbiParam::new(types::I128)], &[
100+
from,
101+
])[0]
118102
} else if to_ty == types::I8 || to_ty == types::I16 {
119103
// FIXME implement fcvt_to_*int_sat.i8/i16
120104
let val = if to_signed {

compiler/rustc_codegen_cranelift/src/codegen_i128.rs

+8-22
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,14 @@ pub(crate) fn maybe_codegen<'tcx>(
3333
(BinOp::Rem, true) => "__modti3",
3434
_ => unreachable!(),
3535
};
36-
if fx.tcx.sess.target.is_like_windows {
37-
let args = [lhs.load_scalar(fx), rhs.load_scalar(fx)];
38-
let ret = fx.lib_call(
39-
name,
40-
vec![AbiParam::new(types::I128), AbiParam::new(types::I128)],
41-
vec![AbiParam::new(types::I64X2)],
42-
&args,
43-
)[0];
44-
// FIXME(bytecodealliance/wasmtime#6104) use bitcast instead of store to get from i64x2 to i128
45-
let ret_place = CPlace::new_stack_slot(fx, lhs.layout());
46-
ret_place.to_ptr().store(fx, ret, MemFlags::trusted());
47-
Some(ret_place.to_cvalue(fx))
48-
} else {
49-
let args = [lhs.load_scalar(fx), rhs.load_scalar(fx)];
50-
let ret_val = fx.lib_call(
51-
name,
52-
vec![AbiParam::new(types::I128), AbiParam::new(types::I128)],
53-
vec![AbiParam::new(types::I128)],
54-
&args,
55-
)[0];
56-
Some(CValue::by_val(ret_val, lhs.layout()))
57-
}
36+
let args = [lhs.load_scalar(fx), rhs.load_scalar(fx)];
37+
let ret_val = fx.lib_call(
38+
name,
39+
vec![AbiParam::new(types::I128), AbiParam::new(types::I128)],
40+
vec![AbiParam::new(types::I128)],
41+
&args,
42+
)[0];
43+
Some(CValue::by_val(ret_val, lhs.layout()))
5844
}
5945
BinOp::Lt | BinOp::Le | BinOp::Eq | BinOp::Ge | BinOp::Gt | BinOp::Ne | BinOp::Cmp => None,
6046
BinOp::Shl | BinOp::ShlUnchecked | BinOp::Shr | BinOp::ShrUnchecked => None,

compiler/rustc_parse/src/errors.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2830,19 +2830,21 @@ pub(crate) struct DynAfterMut {
28302830
pub(crate) struct FnPointerCannotBeConst {
28312831
#[primary_span]
28322832
pub span: Span,
2833-
#[suggestion(code = "", applicability = "maybe-incorrect", style = "verbose")]
28342833
#[label]
28352834
pub qualifier: Span,
2835+
#[suggestion(code = "", applicability = "maybe-incorrect", style = "verbose")]
2836+
pub suggestion: Span,
28362837
}
28372838

28382839
#[derive(Diagnostic)]
28392840
#[diag(parse_fn_pointer_cannot_be_async)]
28402841
pub(crate) struct FnPointerCannotBeAsync {
28412842
#[primary_span]
28422843
pub span: Span,
2843-
#[suggestion(code = "", applicability = "maybe-incorrect", style = "verbose")]
28442844
#[label]
28452845
pub qualifier: Span,
2846+
#[suggestion(code = "", applicability = "maybe-incorrect", style = "verbose")]
2847+
pub suggestion: Span,
28462848
}
28472849

28482850
#[derive(Diagnostic)]

compiler/rustc_parse/src/parser/ty.rs

+46-4
Original file line numberDiff line numberDiff line change
@@ -609,16 +609,58 @@ impl<'a> Parser<'a> {
609609
let span_start = self.token.span;
610610
let ast::FnHeader { ext, safety, constness, coroutine_kind } =
611611
self.parse_fn_front_matter(&inherited_vis, Case::Sensitive)?;
612+
let fn_start_lo = self.prev_token.span.lo();
612613
if self.may_recover() && self.token == TokenKind::Lt {
613614
self.recover_fn_ptr_with_generics(lo, &mut params, param_insertion_point)?;
614615
}
615616
let decl = self.parse_fn_decl(|_| false, AllowPlus::No, recover_return_sign)?;
616617
let whole_span = lo.to(self.prev_token.span);
617-
if let ast::Const::Yes(span) = constness {
618-
self.dcx().emit_err(FnPointerCannotBeConst { span: whole_span, qualifier: span });
618+
619+
// Order/parsing of "front matter" follows:
620+
// `<constness> <coroutine_kind> <safety> <extern> fn()`
621+
// ^ ^ ^ ^ ^
622+
// | | | | fn_start_lo
623+
// | | | ext_sp.lo
624+
// | | safety_sp.lo
625+
// | coroutine_sp.lo
626+
// const_sp.lo
627+
if let ast::Const::Yes(const_span) = constness {
628+
let next_token_lo = if let Some(
629+
ast::CoroutineKind::Async { span, .. }
630+
| ast::CoroutineKind::Gen { span, .. }
631+
| ast::CoroutineKind::AsyncGen { span, .. },
632+
) = coroutine_kind
633+
{
634+
span.lo()
635+
} else if let ast::Safety::Unsafe(span) | ast::Safety::Safe(span) = safety {
636+
span.lo()
637+
} else if let ast::Extern::Implicit(span) | ast::Extern::Explicit(_, span) = ext {
638+
span.lo()
639+
} else {
640+
fn_start_lo
641+
};
642+
let sugg_span = const_span.with_hi(next_token_lo);
643+
self.dcx().emit_err(FnPointerCannotBeConst {
644+
span: whole_span,
645+
qualifier: const_span,
646+
suggestion: sugg_span,
647+
});
619648
}
620-
if let Some(ast::CoroutineKind::Async { span, .. }) = coroutine_kind {
621-
self.dcx().emit_err(FnPointerCannotBeAsync { span: whole_span, qualifier: span });
649+
if let Some(ast::CoroutineKind::Async { span: async_span, .. }) = coroutine_kind {
650+
let next_token_lo = if let ast::Safety::Unsafe(span) | ast::Safety::Safe(span) = safety
651+
{
652+
span.lo()
653+
} else if let ast::Extern::Implicit(span) | ast::Extern::Explicit(_, span) = ext {
654+
span.lo()
655+
} else {
656+
fn_start_lo
657+
};
658+
let sugg_span = async_span.with_hi(next_token_lo);
659+
self.dcx().emit_err(FnPointerCannotBeAsync {
660+
span: whole_span,
661+
qualifier: async_span,
662+
suggestion: sugg_span,
663+
});
622664
}
623665
// FIXME(gen_blocks): emit a similar error for `gen fn()`
624666
let decl_span = span_start.to(self.prev_token.span);

0 commit comments

Comments
 (0)