Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
));
let can_subst = |ty: Ty<'tcx>| {
// Normalize before comparing to see through type aliases and projections.
let old_ty = ty::EarlyBinder::bind(ty).instantiate(tcx, generic_args);
let new_ty = ty::EarlyBinder::bind(ty).instantiate(tcx, new_args);
let old_ty = ty::EarlyBinder::bind(tcx, ty).instantiate(tcx, generic_args);
let new_ty = ty::EarlyBinder::bind(tcx, ty).instantiate(tcx, new_args);
if let Ok(old_ty) = tcx.try_normalize_erasing_regions(
self.infcx.typing_env(self.infcx.param_env),
old_ty,
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_borrowck/src/diagnostics/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
"non-defining use of `{}` in the defining scope",
Ty::new_opaque(
infcx.tcx,
ty::IsRigid::No,
opaque_type_key.def_id.to_def_id(),
opaque_type_key.args
)
Expand Down Expand Up @@ -220,7 +221,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for FindOpaqueRegion<'_, 'tcx> {
fn visit_ty(&mut self, ty: Ty<'tcx>) -> Self::Result {
// If we find an opaque in a local ty, then for each of its captured regions,
// try to find a path between that captured regions and our borrow region...
if let ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, args, .. }) = *ty.kind()
if let ty::Alias(_, ty::AliasTy { kind: ty::Opaque { def_id }, args, .. }) = *ty.kind()
&& let hir::OpaqueTyOrigin::FnReturn { parent, in_trait_or_impl: None } =
self.tcx.opaque_ty_origin(def_id)
{
Expand Down Expand Up @@ -277,7 +278,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for CheckExplicitRegionMentionAndCollectGen

fn visit_ty(&mut self, ty: Ty<'tcx>) -> Self::Result {
match *ty.kind() {
ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, args, .. }) => {
ty::Alias(_, ty::AliasTy { kind: ty::Opaque { def_id }, args, .. }) => {
if self.seen_opaques.insert(def_id) {
for (bound, _) in self
.tcx
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/diagnostics/region_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
let ErrorConstraintInfo { outlived_fr, span, .. } = errci;

let mut output_ty = self.regioncx.universal_regions().unnormalized_output_ty;
if let ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, .. }) = *output_ty.kind() {
if let ty::Alias(_, ty::AliasTy { kind: ty::Opaque { def_id }, .. }) = *output_ty.kind() {
output_ty = self.infcx.tcx.type_of(def_id).instantiate_identity().skip_norm_wip()
};

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1928,7 +1928,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
| ty::Never
| ty::Tuple(_)
| ty::UnsafeBinder(_)
| ty::Alias(_)
| ty::Alias(_, _)
| ty::Param(_)
| ty::Bound(_, _)
| ty::Infer(_)
Expand Down Expand Up @@ -1970,7 +1970,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
| ty::CoroutineWitness(..)
| ty::Never
| ty::UnsafeBinder(_)
| ty::Alias(_)
| ty::Alias(_, _)
| ty::Param(_)
| ty::Bound(_, _)
| ty::Infer(_)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for CollectMemberConstraintsVisitor<'_, '_,
| ty::CoroutineClosure(def_id, args)
| ty::Coroutine(def_id, args) => self.visit_closure_args(def_id, args),

ty::Alias(ty::AliasTy { kind, args, .. })
ty::Alias(_, ty::AliasTy { kind, args, .. })
if let Some(variances) = self.cx().opt_alias_variances(kind) =>
{
// Skip lifetime parameters that are not captured, since they do
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_borrowck/src/region_infer/opaque_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ fn compute_definition_site_hidden_types_from_defining_uses<'tcx>(
// usage of the opaque type and we can ignore it. This check is mirrored in typeck's
// writeback.
if !rcx.infcx.tcx.use_typing_mode_post_typeck_until_borrowck() {
if let &ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, args, .. }) =
if let &ty::Alias(_, ty::AliasTy { kind: ty::Opaque { def_id }, args, .. }) =
hidden_type.ty.skip_binder().kind()
&& def_id == opaque_type_key.def_id.to_def_id()
&& args == opaque_type_key.args
Expand Down Expand Up @@ -500,7 +500,7 @@ impl<'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for ToArgRegionsFolder<'_, 'tcx> {
Ty::new_coroutine(tcx, def_id, self.fold_closure_args(def_id, args)?)
}

ty::Alias(ty::AliasTy { kind, args, .. })
ty::Alias(_, ty::AliasTy { kind, args, .. })
if let Some(variances) = tcx.opt_alias_variances(kind) =>
{
let args = tcx.mk_args_from_iter(std::iter::zip(variances, args.iter()).map(
Expand All @@ -512,7 +512,7 @@ impl<'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for ToArgRegionsFolder<'_, 'tcx> {
}
},
))?;
ty::AliasTy::new_from_args(tcx, kind, args).to_ty(tcx)
ty::AliasTy::new_from_args(tcx, kind, args).to_ty(tcx, ty::IsRigid::No)
}

_ => ty.try_super_fold_with(self)?,
Expand Down Expand Up @@ -541,7 +541,7 @@ pub(crate) fn apply_definition_site_hidden_types<'tcx>(
for &(key, hidden_type) in opaque_types {
let Some(expected) = hidden_types.get(&key.def_id) else {
if !tcx.use_typing_mode_post_typeck_until_borrowck() {
if let &ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, args, .. }) =
if let &ty::Alias(_, ty::AliasTy { kind: ty::Opaque { def_id }, args, .. }) =
hidden_type.ty.kind()
&& def_id == key.def_id.to_def_id()
&& args == key.args
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_borrowck/src/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
// Necessary for non-trivial patterns whose user-type annotation is an opaque type,
// e.g. `let (_a,): Tait = whatever`, see #105897
if !self.infcx.next_trait_solver()
&& let ty::Alias(ty::AliasTy { kind: ty::Opaque { .. }, .. }) =
&& let ty::Alias(_, ty::AliasTy { kind: ty::Opaque { .. }, .. }) =
curr_projected_ty.ty.kind()
{
// There is nothing that we can compare here if we go through an opaque type.
Expand Down Expand Up @@ -1760,7 +1760,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
let tcx = self.tcx();
let maybe_uneval = match constant.const_ {
Const::Ty(_, ct) => match ct.kind() {
ty::ConstKind::Unevaluated(uv) => match uv.kind {
ty::ConstKind::Unevaluated(_, uv) => match uv.kind {
ty::UnevaluatedConstKind::Projection { def_id }
| ty::UnevaluatedConstKind::Inherent { def_id }
| ty::UnevaluatedConstKind::Free { def_id }
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_borrowck/src/type_check/relate_tys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ impl<'a, 'b, 'tcx> NllTypeRelating<'a, 'b, 'tcx> {
};

let (a, b) = match (a.kind(), b.kind()) {
(&ty::Alias(ty::AliasTy { kind: ty::Opaque { .. }, .. }), _) => {
(&ty::Alias(_, ty::AliasTy { kind: ty::Opaque { .. }, .. }), _) => {
(a, enable_subtyping(b, true)?)
}
(_, &ty::Alias(ty::AliasTy { kind: ty::Opaque { .. }, .. })) => {
(_, &ty::Alias(_, ty::AliasTy { kind: ty::Opaque { .. }, .. })) => {
(enable_subtyping(a, false)?, b)
}
_ => unreachable!(
Expand Down Expand Up @@ -386,8 +386,8 @@ impl<'b, 'tcx> TypeRelation<TyCtxt<'tcx>> for NllTypeRelating<'_, 'b, 'tcx> {
}

(
&ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id: a_def_id }, .. }),
&ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id: b_def_id }, .. }),
&ty::Alias(_, ty::AliasTy { kind: ty::Opaque { def_id: a_def_id }, .. }),
&ty::Alias(_, ty::AliasTy { kind: ty::Opaque { def_id: b_def_id }, .. }),
) if a_def_id == b_def_id || infcx.next_trait_solver() => {
super_combine_tys(&infcx.infcx, self, a, b).map(|_| ()).or_else(|err| {
// This behavior is only there for the old solver, the new solver
Expand All @@ -401,8 +401,8 @@ impl<'b, 'tcx> TypeRelation<TyCtxt<'tcx>> for NllTypeRelating<'_, 'b, 'tcx> {
if a_def_id.is_local() { self.relate_opaques(a, b) } else { Err(err) }
})?;
}
(&ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, .. }), _)
| (_, &ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, .. }))
(&ty::Alias(_, ty::AliasTy { kind: ty::Opaque { def_id }, .. }), _)
| (_, &ty::Alias(_, ty::AliasTy { kind: ty::Opaque { def_id }, .. }))
if def_id.is_local() && !self.type_checker.infcx.next_trait_solver() =>
{
self.relate_opaques(a, b)?;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_cranelift/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
self.instance.instantiate_mir_and_normalize_erasing_regions(
self.tcx,
ty::TypingEnv::fully_monomorphized(),
ty::EarlyBinder::bind(value),
ty::EarlyBinder::bind(self.tcx, value),
)
}

Expand Down
186 changes: 186 additions & 0 deletions compiler/rustc_codegen_llvm/src/debuginfo/create_scope_map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
use std::collections::hash_map::Entry;

use rustc_codegen_ssa::mir::debuginfo::{DebugScope, FunctionDebugContext};
use rustc_codegen_ssa::traits::*;
use rustc_data_structures::fx::FxHashMap;
use rustc_index::bit_set::DenseBitSet;
use rustc_middle::mir::{Body, SourceScope};
use rustc_middle::ty::layout::{FnAbiOf, HasTypingEnv};
use rustc_middle::ty::{self, Instance};
use rustc_session::config::DebugInfo;
use rustc_span::{BytePos, DUMMY_SP, hygiene};

use super::metadata::file_metadata;
use super::utils::DIB;
use crate::common::CodegenCx;
use crate::llvm;
use crate::llvm::debuginfo::{DILocation, DIScope};

/// Produces DIScope DIEs for each MIR Scope which has variables defined in it.
// FIXME(eddyb) almost all of this should be in `rustc_codegen_ssa::mir::debuginfo`.
pub(crate) fn compute_mir_scopes<'ll, 'tcx>(
cx: &CodegenCx<'ll, 'tcx>,
instance: Instance<'tcx>,
mir: &Body<'tcx>,
debug_context: &mut FunctionDebugContext<'tcx, &'ll DIScope, &'ll DILocation>,
) {
// Find all scopes with variables defined in them.
let variables = if cx.sess().opts.debuginfo == DebugInfo::Full {
let mut vars = DenseBitSet::new_empty(mir.source_scopes.len());
// FIXME(eddyb) take into account that arguments always have debuginfo,
// irrespective of their name (assuming full debuginfo is enabled).
// NOTE(eddyb) actually, on second thought, those are always in the
// function scope, which always exists.
for var_debug_info in &mir.var_debug_info {
vars.insert(var_debug_info.source_info.scope);
}
Some(vars)
} else {
// Nothing to emit, of course.
None
};
let mut instantiated = DenseBitSet::new_empty(mir.source_scopes.len());
let mut discriminators = FxHashMap::default();
// Instantiate all scopes.
for scope in mir.source_scopes.indices() {
make_mir_scope(
cx,
instance,
mir,
&variables,
debug_context,
&mut instantiated,
&mut discriminators,
scope,
);
}
assert!(instantiated.count() == mir.source_scopes.len());
}

fn make_mir_scope<'ll, 'tcx>(
cx: &CodegenCx<'ll, 'tcx>,
instance: Instance<'tcx>,
mir: &Body<'tcx>,
variables: &Option<DenseBitSet<SourceScope>>,
debug_context: &mut FunctionDebugContext<'tcx, &'ll DIScope, &'ll DILocation>,
instantiated: &mut DenseBitSet<SourceScope>,
discriminators: &mut FxHashMap<BytePos, u32>,
scope: SourceScope,
) {
if instantiated.contains(scope) {
return;
}

let scope_data = &mir.source_scopes[scope];
let parent_scope = if let Some(parent) = scope_data.parent_scope {
make_mir_scope(
cx,
instance,
mir,
variables,
debug_context,
instantiated,
discriminators,
parent,
);
debug_context.scopes[parent]
} else {
// The root is the function itself.
let file = cx.sess().source_map().lookup_source_file(mir.span.lo());
debug_context.scopes[scope] = DebugScope {
file_start_pos: file.start_pos,
file_end_pos: file.end_position(),
..debug_context.scopes[scope]
};
instantiated.insert(scope);
return;
};

if let Some(vars) = variables
&& !vars.contains(scope)
&& scope_data.inlined.is_none()
{
// Do not create a DIScope if there are no variables defined in this
// MIR `SourceScope`, and it's not `inlined`, to avoid debuginfo bloat.
debug_context.scopes[scope] = parent_scope;
instantiated.insert(scope);
return;
}

let loc = cx.lookup_debug_loc(scope_data.span.lo());
let file_metadata = file_metadata(cx, &loc.file);

let dbg_scope = match scope_data.inlined {
Some((callee, _)) => {
// FIXME(eddyb) this would be `self.monomorphize(&callee)`
// if this is moved to `rustc_codegen_ssa::mir::debuginfo`.
let callee = cx.tcx.instantiate_and_normalize_erasing_regions(
instance.args,
cx.typing_env(),
ty::EarlyBinder::bind(cx.tcx, callee),
);
debug_context.inlined_function_scopes.entry(callee).or_insert_with(|| {
let callee_fn_abi = cx.fn_abi_of_instance(callee, ty::List::empty());
cx.dbg_scope_fn(callee, callee_fn_abi, None)
})
}
None => unsafe {
llvm::LLVMDIBuilderCreateLexicalBlock(
DIB(cx),
parent_scope.dbg_scope,
file_metadata,
loc.line,
loc.col,
)
},
};

let inlined_at = scope_data.inlined.map(|(_, callsite_span)| {
let callsite_span = hygiene::walk_chain_collapsed(callsite_span, mir.span);
let callsite_scope = parent_scope.adjust_dbg_scope_for_span(cx, callsite_span);
let loc = cx.dbg_loc(callsite_scope, parent_scope.inlined_at, callsite_span);

// NB: In order to produce proper debug info for variables (particularly
// arguments) in multiply-inlined functions, LLVM expects to see a single
// DILocalVariable with multiple different DILocations in the IR. While
// the source information for each DILocation would be identical, their
// inlinedAt attributes will be unique to the particular callsite.
//
// We generate DILocations here based on the callsite's location in the
// source code. A single location in the source code usually can't
// produce multiple distinct calls so this mostly works, until
// macros get involved. A macro can generate multiple calls
// at the same span, which breaks the assumption that we're going to
// produce a unique DILocation for every scope we process here. We
// have to explicitly add discriminators if we see inlines into the
// same source code location.
//
// Note further that we can't key this hashtable on the span itself,
// because these spans could have distinct SyntaxContexts. We have
// to key on exactly what we're giving to LLVM.
match discriminators.entry(callsite_span.lo()) {
Entry::Occupied(mut o) => {
*o.get_mut() += 1;
// NB: We have to emit *something* here or we'll fail LLVM IR verification
// in at least some circumstances (see issue #135322) so if the required
// discriminant cannot be encoded fall back to the dummy location.
unsafe { llvm::LLVMRustDILocationCloneWithBaseDiscriminator(loc, *o.get()) }
.unwrap_or_else(|| {
cx.dbg_loc(callsite_scope, parent_scope.inlined_at, DUMMY_SP)
})
}
Entry::Vacant(v) => {
v.insert(0);
loc
}
}
});

debug_context.scopes[scope] = DebugScope {
dbg_scope,
inlined_at: inlined_at.or(parent_scope.inlined_at),
file_start_pos: loc.file.start_pos,
file_end_pos: loc.file.end_position(),
};
instantiated.insert(scope);
}
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_ssa/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
self.instance.instantiate_mir_and_normalize_erasing_regions(
self.cx.tcx(),
self.cx.typing_env(),
ty::EarlyBinder::bind(value),
ty::EarlyBinder::bind(self.cx.tcx(), value),
)
}
}
Expand Down Expand Up @@ -219,7 +219,7 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
let monomorphized_mir = instance.instantiate_mir_and_normalize_erasing_regions(
tcx,
ty::TypingEnv::fully_monomorphized(),
ty::EarlyBinder::bind(mir.clone()),
ty::EarlyBinder::bind(tcx, mir.clone()),
);
mir = tcx.arena.alloc(optimize_use_clone::<Bx>(cx, monomorphized_mir));
}
Expand Down
Loading
Loading