Skip to content

Commit 7bb106f

Browse files
committed
Auto merge of rust-lang#76786 - Dylan-DPC:rollup-x6p60m6, r=Dylan-DPC
Rollup of 10 pull requests Successful merges: - rust-lang#76669 (Prefer asm! over llvm_asm! in core) - rust-lang#76675 (Small improvements to asm documentation) - rust-lang#76681 (remove orphaned files) - rust-lang#76694 (Introduce a PartitioningCx struct) - rust-lang#76695 (fix syntax error in suggesting generic constraint in trait parameter) - rust-lang#76699 (improve const infer error) - rust-lang#76707 (Simplify iter flatten struct doc) - rust-lang#76710 (:arrow_up: rust-analyzer) - rust-lang#76714 (Small docs improvements) - rust-lang#76717 (Fix generating rustc docs with non-default lib directory.) Failed merges: r? `@ghost`
2 parents 5fae569 + f631293 commit 7bb106f

35 files changed

+290
-155
lines changed

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+20-7
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ use rustc_hir::def::{DefKind, Namespace};
66
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
77
use rustc_hir::{Body, Expr, ExprKind, FnRetTy, HirId, Local, Pat};
88
use rustc_middle::hir::map::Map;
9+
use rustc_middle::infer::unify_key::ConstVariableOriginKind;
910
use rustc_middle::ty::print::Print;
1011
use rustc_middle::ty::subst::{GenericArg, GenericArgKind};
11-
use rustc_middle::ty::{self, DefIdTree, Ty};
12+
use rustc_middle::ty::{self, DefIdTree, InferConst, Ty};
1213
use rustc_span::source_map::DesugaringKind;
1314
use rustc_span::symbol::kw;
1415
use rustc_span::Span;
@@ -569,14 +570,26 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
569570
local_visitor.visit_expr(expr);
570571
}
571572

573+
let mut param_name = None;
574+
let span = if let ty::ConstKind::Infer(InferConst::Var(vid)) = ct.val {
575+
let origin = self.inner.borrow_mut().const_unification_table().probe_value(vid).origin;
576+
if let ConstVariableOriginKind::ConstParameterDefinition(param) = origin.kind {
577+
param_name = Some(param);
578+
}
579+
origin.span
580+
} else {
581+
local_visitor.target_span
582+
};
583+
572584
let error_code = error_code.into();
573-
let mut err = self.tcx.sess.struct_span_err_with_code(
574-
local_visitor.target_span,
575-
"type annotations needed",
576-
error_code,
577-
);
585+
let mut err =
586+
self.tcx.sess.struct_span_err_with_code(span, "type annotations needed", error_code);
578587

579-
err.note("unable to infer the value of a const parameter");
588+
if let Some(param_name) = param_name {
589+
err.note(&format!("cannot infer the value of the const parameter `{}`", param_name));
590+
} else {
591+
err.note("unable to infer the value of a const parameter");
592+
}
580593

581594
err
582595
}

compiler/rustc_middle/src/infer/unify_key.rs

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ pub struct ConstVariableOrigin {
124124
pub enum ConstVariableOriginKind {
125125
MiscVariable,
126126
ConstInference,
127+
// FIXME(const_generics): Consider storing the `DefId` of the param here.
127128
ConstParameterDefinition(Symbol),
128129
SubstitutionPlaceholder,
129130
}

compiler/rustc_middle/src/mir/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2285,8 +2285,8 @@ impl<'tcx> Debug for Rvalue<'tcx> {
22852285
/// Constants
22862286
///
22872287
/// Two constants are equal if they are the same constant. Note that
2288-
/// this does not necessarily mean that they are "==" in Rust -- in
2289-
/// particular one must be wary of `NaN`!
2288+
/// this does not necessarily mean that they are `==` in Rust -- in
2289+
/// particular, one must be wary of `NaN`!
22902290
22912291
#[derive(Clone, Copy, PartialEq, TyEncodable, TyDecodable, HashStable)]
22922292
pub struct Constant<'tcx> {

compiler/rustc_middle/src/ty/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ use std::mem;
6666
use std::ops::{Bound, Deref};
6767
use std::sync::Arc;
6868

69-
/// A type that is not publicly constructable. This prevents people from making `TyKind::Error`
70-
/// except through `tcx.err*()`, which are in this module.
69+
/// A type that is not publicly constructable. This prevents people from making [`TyKind::Error`]s
70+
/// except through the error-reporting functions on a [`tcx`][TyCtxt].
7171
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
7272
#[derive(TyEncodable, TyDecodable, HashStable)]
7373
pub struct DelaySpanBugEmitted(());

compiler/rustc_middle/src/ty/diagnostics.rs

+46-20
Original file line numberDiff line numberDiff line change
@@ -202,33 +202,59 @@ pub fn suggest_constraining_type_param(
202202
// Suggestion:
203203
// fn foo<T>(t: T) where T: Foo, T: Bar {... }
204204
// - insert: `, T: Zar`
205+
//
206+
// Additionally, there may be no `where` clause whatsoever in the case that this was
207+
// reached because the generic parameter has a default:
208+
//
209+
// Message:
210+
// trait Foo<T=()> {... }
211+
// - help: consider further restricting this type parameter with `where T: Zar`
212+
//
213+
// Suggestion:
214+
// trait Foo<T=()> where T: Zar {... }
215+
// - insert: `where T: Zar`
205216

206-
let mut param_spans = Vec::new();
217+
if matches!(param.kind, hir::GenericParamKind::Type { default: Some(_), .. })
218+
&& generics.where_clause.predicates.len() == 0
219+
{
220+
// Suggest a bound, but there is no existing `where` clause *and* the type param has a
221+
// default (`<T=Foo>`), so we suggest adding `where T: Bar`.
222+
err.span_suggestion_verbose(
223+
generics.where_clause.tail_span_for_suggestion(),
224+
&msg_restrict_type_further,
225+
format!(" where {}: {}", param_name, constraint),
226+
Applicability::MachineApplicable,
227+
);
228+
} else {
229+
let mut param_spans = Vec::new();
207230

208-
for predicate in generics.where_clause.predicates {
209-
if let WherePredicate::BoundPredicate(WhereBoundPredicate {
210-
span, bounded_ty, ..
211-
}) = predicate
212-
{
213-
if let TyKind::Path(QPath::Resolved(_, path)) = &bounded_ty.kind {
214-
if let Some(segment) = path.segments.first() {
215-
if segment.ident.to_string() == param_name {
216-
param_spans.push(span);
231+
for predicate in generics.where_clause.predicates {
232+
if let WherePredicate::BoundPredicate(WhereBoundPredicate {
233+
span,
234+
bounded_ty,
235+
..
236+
}) = predicate
237+
{
238+
if let TyKind::Path(QPath::Resolved(_, path)) = &bounded_ty.kind {
239+
if let Some(segment) = path.segments.first() {
240+
if segment.ident.to_string() == param_name {
241+
param_spans.push(span);
242+
}
217243
}
218244
}
219245
}
220246
}
221-
}
222247

223-
match &param_spans[..] {
224-
&[&param_span] => suggest_restrict(param_span.shrink_to_hi()),
225-
_ => {
226-
err.span_suggestion_verbose(
227-
generics.where_clause.tail_span_for_suggestion(),
228-
&msg_restrict_type_further,
229-
format!(", {}: {}", param_name, constraint),
230-
Applicability::MachineApplicable,
231-
);
248+
match &param_spans[..] {
249+
&[&param_span] => suggest_restrict(param_span.shrink_to_hi()),
250+
_ => {
251+
err.span_suggestion_verbose(
252+
generics.where_clause.tail_span_for_suggestion(),
253+
&msg_restrict_type_further,
254+
format!(", {}: {}", param_name, constraint),
255+
Applicability::MachineApplicable,
256+
);
257+
}
232258
}
233259
}
234260

compiler/rustc_middle/src/ty/sty.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1233,13 +1233,13 @@ rustc_index::newtype_index! {
12331233
/// particular, imagine a type like this:
12341234
///
12351235
/// for<'a> fn(for<'b> fn(&'b isize, &'a isize), &'a char)
1236-
/// ^ ^ | | |
1237-
/// | | | | |
1238-
/// | +------------+ 0 | |
1239-
/// | | |
1240-
/// +--------------------------------+ 1 |
1241-
/// | |
1242-
/// +------------------------------------------+ 0
1236+
/// ^ ^ | | |
1237+
/// | | | | |
1238+
/// | +------------+ 0 | |
1239+
/// | | |
1240+
/// +----------------------------------+ 1 |
1241+
/// | |
1242+
/// +----------------------------------------------+ 0
12431243
///
12441244
/// In this type, there are two binders (the outer fn and the inner
12451245
/// fn). We need to be able to determine, for any given region, which

compiler/rustc_mir/src/monomorphize/partitioning/default.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_middle::ty::print::characteristic_def_id_of_type;
1111
use rustc_middle::ty::{self, DefIdTree, InstanceDef, TyCtxt};
1212
use rustc_span::symbol::Symbol;
1313

14+
use super::PartitioningCx;
1415
use crate::monomorphize::collector::InliningMap;
1516
use crate::monomorphize::partitioning::merging;
1617
use crate::monomorphize::partitioning::{
@@ -22,35 +23,36 @@ pub struct DefaultPartitioning;
2223
impl<'tcx> Partitioner<'tcx> for DefaultPartitioning {
2324
fn place_root_mono_items(
2425
&mut self,
25-
tcx: TyCtxt<'tcx>,
26+
cx: &PartitioningCx<'_, 'tcx>,
2627
mono_items: &mut dyn Iterator<Item = MonoItem<'tcx>>,
2728
) -> PreInliningPartitioning<'tcx> {
2829
let mut roots = FxHashSet::default();
2930
let mut codegen_units = FxHashMap::default();
30-
let is_incremental_build = tcx.sess.opts.incremental.is_some();
31+
let is_incremental_build = cx.tcx.sess.opts.incremental.is_some();
3132
let mut internalization_candidates = FxHashSet::default();
3233

3334
// Determine if monomorphizations instantiated in this crate will be made
3435
// available to downstream crates. This depends on whether we are in
3536
// share-generics mode and whether the current crate can even have
3637
// downstream crates.
37-
let export_generics = tcx.sess.opts.share_generics() && tcx.local_crate_exports_generics();
38+
let export_generics =
39+
cx.tcx.sess.opts.share_generics() && cx.tcx.local_crate_exports_generics();
3840

39-
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);
41+
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(cx.tcx);
4042
let cgu_name_cache = &mut FxHashMap::default();
4143

4244
for mono_item in mono_items {
43-
match mono_item.instantiation_mode(tcx) {
45+
match mono_item.instantiation_mode(cx.tcx) {
4446
InstantiationMode::GloballyShared { .. } => {}
4547
InstantiationMode::LocalCopy => continue,
4648
}
4749

48-
let characteristic_def_id = characteristic_def_id_of_mono_item(tcx, mono_item);
50+
let characteristic_def_id = characteristic_def_id_of_mono_item(cx.tcx, mono_item);
4951
let is_volatile = is_incremental_build && mono_item.is_generic_fn();
5052

5153
let codegen_unit_name = match characteristic_def_id {
5254
Some(def_id) => compute_codegen_unit_name(
53-
tcx,
55+
cx.tcx,
5456
cgu_name_builder,
5557
def_id,
5658
is_volatile,
@@ -65,7 +67,7 @@ impl<'tcx> Partitioner<'tcx> for DefaultPartitioning {
6567

6668
let mut can_be_internalized = true;
6769
let (linkage, visibility) = mono_item_linkage_and_visibility(
68-
tcx,
70+
cx.tcx,
6971
&mono_item,
7072
&mut can_be_internalized,
7173
export_generics,
@@ -97,17 +99,16 @@ impl<'tcx> Partitioner<'tcx> for DefaultPartitioning {
9799

98100
fn merge_codegen_units(
99101
&mut self,
100-
tcx: TyCtxt<'tcx>,
102+
cx: &PartitioningCx<'_, 'tcx>,
101103
initial_partitioning: &mut PreInliningPartitioning<'tcx>,
102-
target_cgu_count: usize,
103104
) {
104-
merging::merge_codegen_units(tcx, initial_partitioning, target_cgu_count);
105+
merging::merge_codegen_units(cx, initial_partitioning);
105106
}
106107

107108
fn place_inlined_mono_items(
108109
&mut self,
110+
cx: &PartitioningCx<'_, 'tcx>,
109111
initial_partitioning: PreInliningPartitioning<'tcx>,
110-
inlining_map: &InliningMap<'tcx>,
111112
) -> PostInliningPartitioning<'tcx> {
112113
let mut new_partitioning = Vec::new();
113114
let mut mono_item_placements = FxHashMap::default();
@@ -124,7 +125,7 @@ impl<'tcx> Partitioner<'tcx> for DefaultPartitioning {
124125
// Collect all items that need to be available in this codegen unit.
125126
let mut reachable = FxHashSet::default();
126127
for root in old_codegen_unit.items().keys() {
127-
follow_inlining(*root, inlining_map, &mut reachable);
128+
follow_inlining(*root, cx.inlining_map, &mut reachable);
128129
}
129130

130131
let mut new_codegen_unit = CodegenUnit::new(old_codegen_unit.name());
@@ -198,9 +199,8 @@ impl<'tcx> Partitioner<'tcx> for DefaultPartitioning {
198199

199200
fn internalize_symbols(
200201
&mut self,
201-
_tcx: TyCtxt<'tcx>,
202+
cx: &PartitioningCx<'_, 'tcx>,
202203
partitioning: &mut PostInliningPartitioning<'tcx>,
203-
inlining_map: &InliningMap<'tcx>,
204204
) {
205205
if partitioning.codegen_units.len() == 1 {
206206
// Fast path for when there is only one codegen unit. In this case we
@@ -218,7 +218,7 @@ impl<'tcx> Partitioner<'tcx> for DefaultPartitioning {
218218
// Build a map from every monomorphization to all the monomorphizations that
219219
// reference it.
220220
let mut accessor_map: FxHashMap<MonoItem<'tcx>, Vec<MonoItem<'tcx>>> = Default::default();
221-
inlining_map.iter_accesses(|accessor, accessees| {
221+
cx.inlining_map.iter_accesses(|accessor, accessees| {
222222
for accessee in accessees {
223223
accessor_map.entry(*accessee).or_default().push(accessor);
224224
}

compiler/rustc_mir/src/monomorphize/partitioning/merging.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ use std::cmp;
33
use rustc_data_structures::fx::FxHashMap;
44
use rustc_hir::def_id::LOCAL_CRATE;
55
use rustc_middle::mir::mono::{CodegenUnit, CodegenUnitNameBuilder};
6-
use rustc_middle::ty::TyCtxt;
76
use rustc_span::symbol::{Symbol, SymbolStr};
87

8+
use super::PartitioningCx;
99
use crate::monomorphize::partitioning::PreInliningPartitioning;
1010

1111
pub fn merge_codegen_units<'tcx>(
12-
tcx: TyCtxt<'tcx>,
12+
cx: &PartitioningCx<'_, 'tcx>,
1313
initial_partitioning: &mut PreInliningPartitioning<'tcx>,
14-
target_cgu_count: usize,
1514
) {
16-
assert!(target_cgu_count >= 1);
15+
assert!(cx.target_cgu_count >= 1);
1716
let codegen_units = &mut initial_partitioning.codegen_units;
1817

1918
// Note that at this point in time the `codegen_units` here may not be in a
@@ -32,7 +31,7 @@ pub fn merge_codegen_units<'tcx>(
3231
codegen_units.iter().map(|cgu| (cgu.name(), vec![cgu.name().as_str()])).collect();
3332

3433
// Merge the two smallest codegen units until the target size is reached.
35-
while codegen_units.len() > target_cgu_count {
34+
while codegen_units.len() > cx.target_cgu_count {
3635
// Sort small cgus to the back
3736
codegen_units.sort_by_cached_key(|cgu| cmp::Reverse(cgu.size_estimate()));
3837
let mut smallest = codegen_units.pop().unwrap();
@@ -56,9 +55,9 @@ pub fn merge_codegen_units<'tcx>(
5655
);
5756
}
5857

59-
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);
58+
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(cx.tcx);
6059

61-
if tcx.sess.opts.incremental.is_some() {
60+
if cx.tcx.sess.opts.incremental.is_some() {
6261
// If we are doing incremental compilation, we want CGU names to
6362
// reflect the path of the source level module they correspond to.
6463
// For CGUs that contain the code of multiple modules because of the
@@ -84,7 +83,7 @@ pub fn merge_codegen_units<'tcx>(
8483

8584
for cgu in codegen_units.iter_mut() {
8685
if let Some(new_cgu_name) = new_cgu_names.get(&cgu.name()) {
87-
if tcx.sess.opts.debugging_opts.human_readable_cgu_names {
86+
if cx.tcx.sess.opts.debugging_opts.human_readable_cgu_names {
8887
cgu.set_name(Symbol::intern(&new_cgu_name));
8988
} else {
9089
// If we don't require CGU names to be human-readable, we

0 commit comments

Comments
 (0)