Skip to content

Commit 40ae0b8

Browse files
committed
Auto merge of rust-lang#118141 - matthiaskrgr:rollup-djkmfwt, r=matthiaskrgr
Rollup of 4 pull requests Successful merges: - rust-lang#117972 (Add VarDebugInfo to Stable MIR) - rust-lang#118110 (Document `DefiningAnchor` a bit more) - rust-lang#118112 (Don't ICE when ambiguity is found when selecting `Index` implementation in typeck) - rust-lang#118135 (Remove quotation from filename in stable_mir) Failed merges: - rust-lang#118012 (Add support for global allocation in smir) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 2f8d81f + a687126 commit 40ae0b8

File tree

8 files changed

+209
-23
lines changed

8 files changed

+209
-23
lines changed

compiler/rustc_hir_typeck/src/expr.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -3067,7 +3067,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30673067
return None;
30683068
};
30693069

3070-
self.commit_if_ok(|_| {
3070+
self.commit_if_ok(|snapshot| {
3071+
let outer_universe = self.universe();
3072+
30713073
let ocx = ObligationCtxt::new(self);
30723074
let impl_args = self.fresh_args_for_item(base_expr.span, impl_def_id);
30733075
let impl_trait_ref =
@@ -3077,7 +3079,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30773079
// Match the impl self type against the base ty. If this fails,
30783080
// we just skip this impl, since it's not particularly useful.
30793081
let impl_trait_ref = ocx.normalize(&cause, self.param_env, impl_trait_ref);
3080-
ocx.eq(&cause, self.param_env, impl_trait_ref.self_ty(), base_ty)?;
3082+
ocx.eq(&cause, self.param_env, base_ty, impl_trait_ref.self_ty())?;
30813083

30823084
// Register the impl's predicates. One of these predicates
30833085
// must be unsatisfied, or else we wouldn't have gotten here
@@ -3113,11 +3115,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
31133115
Ty::new_projection(self.tcx, index_trait_output_def_id, impl_trait_ref.args),
31143116
);
31153117

3116-
let errors = ocx.select_where_possible();
3118+
let true_errors = ocx.select_where_possible();
3119+
3120+
// Do a leak check -- we can't really report report a useful error here,
3121+
// but it at least avoids an ICE when the error has to do with higher-ranked
3122+
// lifetimes.
3123+
self.leak_check(outer_universe, Some(snapshot))?;
3124+
3125+
// Bail if we have ambiguity errors, which we can't report in a useful way.
3126+
let ambiguity_errors = ocx.select_all_or_error();
3127+
if true_errors.is_empty() && !ambiguity_errors.is_empty() {
3128+
return Err(NoSolution);
3129+
}
3130+
31173131
// There should be at least one error reported. If not, we
31183132
// will still delay a span bug in `report_fulfillment_errors`.
31193133
Ok::<_, NoSolution>((
3120-
self.err_ctxt().report_fulfillment_errors(errors),
3134+
self.err_ctxt().report_fulfillment_errors(true_errors),
31213135
impl_trait_ref.args.type_at(1),
31223136
element_ty,
31233137
))

compiler/rustc_middle/src/traits/mod.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -956,13 +956,26 @@ pub enum CodegenObligationError {
956956
FulfillmentError,
957957
}
958958

959+
/// Defines the treatment of opaque types in a given inference context.
960+
///
961+
/// This affects both what opaques are allowed to be defined, but also whether
962+
/// opaques are replaced with inference vars eagerly in the old solver (e.g.
963+
/// in projection, and in the signature during function type-checking).
959964
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, HashStable, TypeFoldable, TypeVisitable)]
960965
pub enum DefiningAnchor {
961-
/// `DefId` of the item.
966+
/// Define opaques which are in-scope of the `LocalDefId`. Also, eagerly
967+
/// replace opaque types in `replace_opaque_types_with_inference_vars`.
962968
Bind(LocalDefId),
963-
/// When opaque types are not resolved, we `Bubble` up, meaning
964-
/// return the opaque/hidden type pair from query, for caller of query to handle it.
969+
/// In contexts where we don't currently know what opaques are allowed to be
970+
/// defined, such as (old solver) canonical queries, we will simply allow
971+
/// opaques to be defined, but "bubble" them up in the canonical response or
972+
/// otherwise treat them to be handled later.
973+
///
974+
/// We do not eagerly replace opaque types in `replace_opaque_types_with_inference_vars`,
975+
/// which may affect what predicates pass and fail in the old trait solver.
965976
Bubble,
966-
/// Used to catch type mismatch errors when handling opaque types.
977+
/// Do not allow any opaques to be defined. This is used to catch type mismatch
978+
/// errors when handling opaque types, and also should be used when we would
979+
/// otherwise reveal opaques (such as [`Reveal::All`] reveal mode).
967980
Error,
968981
}

compiler/rustc_smir/src/rustc_smir/mod.rs

+61-10
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ use rustc_middle::ty::{self, Instance, ParamEnv, ScalarInt, Ty, TyCtxt, Variance
1818
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
1919
use rustc_target::abi::FieldIdx;
2020
use stable_mir::mir::mono::InstanceDef;
21-
use stable_mir::mir::{Body, CopyNonOverlapping, Statement, UserTypeProjection, VariantIdx};
21+
use stable_mir::mir::{
22+
Body, ConstOperand, CopyNonOverlapping, Statement, UserTypeProjection, VarDebugInfoFragment,
23+
VariantIdx,
24+
};
2225
use stable_mir::ty::{
2326
AdtDef, AdtKind, ClosureDef, ClosureKind, Const, ConstId, ConstantKind, EarlyParamRegion,
2427
FloatTy, FnDef, GenericArgs, GenericParamDef, IntTy, LineInfo, Movability, RigidTy, Span,
@@ -69,15 +72,13 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
6972

7073
fn get_filename(&self, span: &Span) -> Filename {
7174
let tables = self.0.borrow();
72-
opaque(
73-
&tables
74-
.tcx
75-
.sess
76-
.source_map()
77-
.span_to_filename(tables[*span])
78-
.display(rustc_span::FileNameDisplayPreference::Local)
79-
.to_string(),
80-
)
75+
tables
76+
.tcx
77+
.sess
78+
.source_map()
79+
.span_to_filename(tables[*span])
80+
.display(rustc_span::FileNameDisplayPreference::Local)
81+
.to_string()
8182
}
8283

8384
fn get_lines(&self, span: &Span) -> LineInfo {
@@ -444,17 +445,67 @@ impl<'tcx> Stable<'tcx> for mir::Body<'tcx> {
444445
})
445446
.collect(),
446447
self.arg_count,
448+
self.var_debug_info.iter().map(|info| info.stable(tables)).collect(),
447449
)
448450
}
449451
}
450452

453+
impl<'tcx> Stable<'tcx> for mir::VarDebugInfo<'tcx> {
454+
type T = stable_mir::mir::VarDebugInfo;
455+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
456+
stable_mir::mir::VarDebugInfo {
457+
name: self.name.to_string(),
458+
source_info: self.source_info.stable(tables),
459+
composite: self.composite.as_ref().map(|composite| composite.stable(tables)),
460+
value: self.value.stable(tables),
461+
argument_index: self.argument_index,
462+
}
463+
}
464+
}
465+
451466
impl<'tcx> Stable<'tcx> for mir::Statement<'tcx> {
452467
type T = stable_mir::mir::Statement;
453468
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
454469
Statement { kind: self.kind.stable(tables), span: self.source_info.span.stable(tables) }
455470
}
456471
}
457472

473+
impl<'tcx> Stable<'tcx> for mir::SourceInfo {
474+
type T = stable_mir::mir::SourceInfo;
475+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
476+
stable_mir::mir::SourceInfo { span: self.span.stable(tables), scope: self.scope.into() }
477+
}
478+
}
479+
480+
impl<'tcx> Stable<'tcx> for mir::VarDebugInfoFragment<'tcx> {
481+
type T = stable_mir::mir::VarDebugInfoFragment;
482+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
483+
VarDebugInfoFragment {
484+
ty: self.ty.stable(tables),
485+
projection: self.projection.iter().map(|e| e.stable(tables)).collect(),
486+
}
487+
}
488+
}
489+
490+
impl<'tcx> Stable<'tcx> for mir::VarDebugInfoContents<'tcx> {
491+
type T = stable_mir::mir::VarDebugInfoContents;
492+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
493+
match self {
494+
mir::VarDebugInfoContents::Place(place) => {
495+
stable_mir::mir::VarDebugInfoContents::Place(place.stable(tables))
496+
}
497+
mir::VarDebugInfoContents::Const(const_operand) => {
498+
let op = ConstOperand {
499+
span: const_operand.span.stable(tables),
500+
user_ty: const_operand.user_ty.map(|index| index.as_usize()),
501+
const_: const_operand.const_.stable(tables),
502+
};
503+
stable_mir::mir::VarDebugInfoContents::Const(op)
504+
}
505+
}
506+
}
507+
}
508+
458509
impl<'tcx> Stable<'tcx> for mir::StatementKind<'tcx> {
459510
type T = stable_mir::mir::StatementKind;
460511
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {

compiler/stable_mir/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pub enum ItemKind {
110110
Const,
111111
}
112112

113-
pub type Filename = Opaque;
113+
pub type Filename = String;
114114

115115
/// Holds information about an item in the crate.
116116
#[derive(Copy, Clone, PartialEq, Eq, Debug)]

compiler/stable_mir/src/mir/body.rs

+47-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::mir::pretty::{function_body, pretty_statement};
22
use crate::ty::{
33
AdtDef, ClosureDef, Const, CoroutineDef, GenericArgs, Movability, Region, RigidTy, Ty, TyKind,
44
};
5-
use crate::{Error, Opaque, Span};
5+
use crate::{Error, Opaque, Span, Symbol};
66
use std::io;
77

88
/// The SMIR representation of a single function.
@@ -19,21 +19,29 @@ pub struct Body {
1919

2020
// The number of arguments this function takes.
2121
pub(super) arg_count: usize,
22+
23+
// Debug information pertaining to user variables, including captures.
24+
pub(super) var_debug_info: Vec<VarDebugInfo>,
2225
}
2326

2427
impl Body {
2528
/// Constructs a `Body`.
2629
///
2730
/// A constructor is required to build a `Body` from outside the crate
2831
/// because the `arg_count` and `locals` fields are private.
29-
pub fn new(blocks: Vec<BasicBlock>, locals: LocalDecls, arg_count: usize) -> Self {
32+
pub fn new(
33+
blocks: Vec<BasicBlock>,
34+
locals: LocalDecls,
35+
arg_count: usize,
36+
var_debug_info: Vec<VarDebugInfo>,
37+
) -> Self {
3038
// If locals doesn't contain enough entries, it can lead to panics in
3139
// `ret_local`, `arg_locals`, and `inner_locals`.
3240
assert!(
3341
locals.len() > arg_count,
3442
"A Body must contain at least a local for the return value and each of the function's arguments"
3543
);
36-
Self { blocks, locals, arg_count }
44+
Self { blocks, locals, arg_count, var_debug_info }
3745
}
3846

3947
/// Return local that holds this function's return value.
@@ -427,6 +435,42 @@ pub struct Place {
427435
pub projection: Vec<ProjectionElem>,
428436
}
429437

438+
#[derive(Clone, Debug, Eq, PartialEq)]
439+
pub struct VarDebugInfo {
440+
pub name: Symbol,
441+
pub source_info: SourceInfo,
442+
pub composite: Option<VarDebugInfoFragment>,
443+
pub value: VarDebugInfoContents,
444+
pub argument_index: Option<u16>,
445+
}
446+
447+
pub type SourceScope = u32;
448+
449+
#[derive(Clone, Debug, Eq, PartialEq)]
450+
pub struct SourceInfo {
451+
pub span: Span,
452+
pub scope: SourceScope,
453+
}
454+
455+
#[derive(Clone, Debug, Eq, PartialEq)]
456+
pub struct VarDebugInfoFragment {
457+
pub ty: Ty,
458+
pub projection: Vec<ProjectionElem>,
459+
}
460+
461+
#[derive(Clone, Debug, Eq, PartialEq)]
462+
pub enum VarDebugInfoContents {
463+
Place(Place),
464+
Const(ConstOperand),
465+
}
466+
467+
#[derive(Clone, Debug, Eq, PartialEq)]
468+
pub struct ConstOperand {
469+
pub span: Span,
470+
pub user_ty: Option<UserTypeAnnotationIndex>,
471+
pub const_: Const,
472+
}
473+
430474
// In MIR ProjectionElem is parameterized on the second Field argument and the Index argument. This
431475
// is so it can be used for both Places (for which the projection elements are of type
432476
// ProjectionElem<Local, Ty>) and user-provided type annotations (for which the projection elements

compiler/stable_mir/src/mir/visit.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,12 @@ pub trait MirVisitor {
128128
self.super_assert_msg(msg, location)
129129
}
130130

131+
fn visit_var_debug_info(&mut self, var_debug_info: &VarDebugInfo) {
132+
self.super_var_debug_info(var_debug_info);
133+
}
134+
131135
fn super_body(&mut self, body: &Body) {
132-
let Body { blocks, locals: _, arg_count } = body;
136+
let Body { blocks, locals: _, arg_count, var_debug_info } = body;
133137

134138
for bb in blocks {
135139
self.visit_basic_block(bb);
@@ -145,6 +149,10 @@ pub trait MirVisitor {
145149
for (idx, arg) in body.inner_locals().iter().enumerate() {
146150
self.visit_local_decl(idx + local_start, arg)
147151
}
152+
153+
for info in var_debug_info.iter() {
154+
self.visit_var_debug_info(info);
155+
}
148156
}
149157

150158
fn super_basic_block(&mut self, bb: &BasicBlock) {
@@ -382,6 +390,24 @@ pub trait MirVisitor {
382390
let _ = args;
383391
}
384392

393+
fn super_var_debug_info(&mut self, var_debug_info: &VarDebugInfo) {
394+
let VarDebugInfo { source_info, composite, value, name: _, argument_index: _ } =
395+
var_debug_info;
396+
self.visit_span(&source_info.span);
397+
let location = Location(source_info.span);
398+
if let Some(composite) = composite {
399+
self.visit_ty(&composite.ty, location);
400+
}
401+
match value {
402+
VarDebugInfoContents::Place(place) => {
403+
self.visit_place(place, PlaceContext::NON_USE, location);
404+
}
405+
VarDebugInfoContents::Const(constant) => {
406+
self.visit_const(&constant.const_, location);
407+
}
408+
}
409+
}
410+
385411
fn super_assert_msg(&mut self, msg: &AssertMessage, location: Location) {
386412
match msg {
387413
AssertMessage::BoundsCheck { len, index } => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Test against ICE in #118111
2+
3+
use std::ops::Index;
4+
5+
struct Map<T, F> {
6+
f: F,
7+
inner: T,
8+
}
9+
10+
impl<T, F, Idx> Index<Idx> for Map<T, F>
11+
where
12+
T: Index<Idx>,
13+
F: FnOnce(&T, Idx) -> Idx,
14+
{
15+
type Output = T::Output;
16+
17+
fn index(&self, index: Idx) -> &Self::Output {
18+
todo!()
19+
}
20+
}
21+
22+
fn main() {
23+
Map { inner: [0_usize], f: |_, i: usize| 1_usize }[0];
24+
//~^ ERROR cannot index into a value of type
25+
// Problem here is that
26+
// `f: |_, i: usize| ...`
27+
// should be
28+
// `f: |_: &_, i: usize| ...`
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0608]: cannot index into a value of type `Map<[usize; 1], {closure@$DIR/bad-index-modulo-higher-ranked-regions.rs:23:32: 23:45}>`
2+
--> $DIR/bad-index-modulo-higher-ranked-regions.rs:23:55
3+
|
4+
LL | Map { inner: [0_usize], f: |_, i: usize| 1_usize }[0];
5+
| ^^^
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0608`.

0 commit comments

Comments
 (0)