Skip to content

WIP: Thread GeneratorKind through to symbol mangling #104333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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_codegen_ssa/src/debuginfo/type_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,8 @@ fn push_unqualified_item_name(
DefPathData::CrateRoot => {
output.push_str(tcx.crate_name(def_id.krate).as_str());
}
DefPathData::ClosureExpr => {
let label = generator_kind_label(tcx.generator_kind(def_id));
DefPathData::ClosureExpr(generator_kind) => {
let label = generator_kind_label(generator_kind);

push_disambiguated_special_name(
label,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_const_eval/src/interpret/eval_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ impl<'mir, 'tcx, Prov: Provenance, Extra> Frame<'mir, 'tcx, Prov, Extra> {
impl<'tcx> fmt::Display for FrameInfo<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
ty::tls::with(|tcx| {
if tcx.def_key(self.instance.def_id()).disambiguated_data.data
== DefPathData::ClosureExpr
if let DefPathData::ClosureExpr(_) =
tcx.def_key(self.instance.def_id()).disambiguated_data.data
{
write!(f, "inside closure")?;
} else {
Expand Down
9 changes: 5 additions & 4 deletions compiler/rustc_hir/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
pub use crate::def_id::DefPathHash;
use crate::def_id::{CrateNum, DefIndex, LocalDefId, StableCrateId, CRATE_DEF_INDEX, LOCAL_CRATE};
use crate::def_path_hash_map::DefPathHashMap;
use crate::GeneratorKind;

use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::StableHasher;
Expand Down Expand Up @@ -272,7 +273,7 @@ pub enum DefPathData {
/// Something in the lifetime namespace.
LifetimeNs(Symbol),
/// A closure expression.
ClosureExpr,
ClosureExpr(Option<GeneratorKind>),

// Subportions of items:
/// Implicit constructor for a unit or tuple-like struct or enum variant.
Expand Down Expand Up @@ -403,8 +404,8 @@ impl DefPathData {
match *self {
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => Some(name),

Impl | ForeignMod | CrateRoot | Use | GlobalAsm | ClosureExpr | Ctor | AnonConst
| ImplTrait => None,
Impl | ForeignMod | CrateRoot | Use | GlobalAsm | ClosureExpr(..) | Ctor
| AnonConst | ImplTrait => None,
}
}

Expand All @@ -420,7 +421,7 @@ impl DefPathData {
ForeignMod => DefPathDataName::Anon { namespace: kw::Extern },
Use => DefPathDataName::Anon { namespace: kw::Use },
GlobalAsm => DefPathDataName::Anon { namespace: sym::global_asm },
ClosureExpr => DefPathDataName::Anon { namespace: sym::closure },
ClosureExpr(..) => DefPathDataName::Anon { namespace: sym::closure },
Ctor => DefPathDataName::Anon { namespace: sym::constructor },
AnonConst => DefPathDataName::Anon { namespace: sym::constant },
ImplTrait => DefPathDataName::Anon { namespace: sym::opaque },
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl<'tcx> InstanceDef<'tcx> {
};
matches!(
tcx.def_key(def_id).disambiguated_data.data,
DefPathData::Ctor | DefPathData::ClosureExpr
DefPathData::Ctor | DefPathData::ClosureExpr(..)
)
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/print/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ pub trait Printer<'tcx>: Sized {

match key.disambiguated_data.data {
// Closures' own generics are only captures, don't print them.
DefPathData::ClosureExpr => {}
DefPathData::ClosureExpr(..) => {}
// This covers both `DefKind::AnonConst` and `DefKind::InlineConst`.
// Anon consts doesn't have their own generics, and inline consts' own
// generics are their inferred types, so don't print them.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1638,7 +1638,7 @@ fn guess_def_namespace(tcx: TyCtxt<'_>, def_id: DefId) -> Namespace {

DefPathData::ValueNs(..)
| DefPathData::AnonConst
| DefPathData::ClosureExpr
| DefPathData::ClosureExpr(..)
| DefPathData::Ctor => Namespace::ValueNS,

DefPathData::MacroNs(..) => Namespace::MacroNS,
Expand Down
19 changes: 15 additions & 4 deletions compiler/rustc_resolve/src/def_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use rustc_ast::*;
use rustc_expand::expand::AstFragment;
use rustc_hir::def_id::LocalDefId;
use rustc_hir::definitions::*;
use rustc_hir::{AsyncGeneratorKind, GeneratorKind};
use rustc_span::hygiene::LocalExpnId;
use rustc_span::symbol::sym;
use rustc_span::Span;
Expand Down Expand Up @@ -151,7 +152,9 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> {
// then the closure_def will never be used, and we should avoid generating a
// def-id for it.
if let Some(body) = body {
let closure_def = self.create_def(closure_id, DefPathData::ClosureExpr, span);
let generator_kind = Some(GeneratorKind::Async(AsyncGeneratorKind::Fn));
let closure_def =
self.create_def(closure_id, DefPathData::ClosureExpr(generator_kind), span);
self.with_parent(closure_def, |this| this.visit_block(body));
}
return;
Expand Down Expand Up @@ -265,16 +268,24 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> {
ExprKind::Closure(_, _, asyncness, ..) => {
// Async closures desugar to closures inside of closures, so
// we must create two defs.
let closure_def = self.create_def(expr.id, DefPathData::ClosureExpr, expr.span);
let closure_def =
self.create_def(expr.id, DefPathData::ClosureExpr(None), expr.span);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The closure could still be a regular generator if the body contains a yield.
It should be included here, otherwise we'll have an inconsistency between the DefPath and HIR.

match asyncness {
Async::Yes { closure_id, .. } => {
self.create_def(closure_id, DefPathData::ClosureExpr, expr.span)
let generator_kind =
Some(GeneratorKind::Async(AsyncGeneratorKind::Closure));
self.create_def(
closure_id,
DefPathData::ClosureExpr(generator_kind),
expr.span,
)
}
Async::No => closure_def,
}
}
ExprKind::Async(_, async_id, _) => {
self.create_def(async_id, DefPathData::ClosureExpr, expr.span)
let generator_kind = Some(GeneratorKind::Async(AsyncGeneratorKind::Block));
self.create_def(async_id, DefPathData::ClosureExpr(generator_kind), expr.span)
}
_ => self.parent_def,
};
Expand Down
30 changes: 15 additions & 15 deletions compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use bitflags::bitflags;
use core::fmt::Display;
use rustc_data_structures::base_n;
use rustc_data_structures::fx::FxHashMap;
use rustc_hir as hir;
use rustc_hir::definitions::DefPathData;
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, SubstsRef};
use rustc_middle::ty::{
self, Binder, Const, ExistentialPredicate, FloatTy, FnSig, IntTy, List, Region, RegionKind,
Expand Down Expand Up @@ -388,20 +388,20 @@ fn encode_ty_name<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> String {
def_path.data.reverse();
for disambiguated_data in &def_path.data {
s.push('N');
s.push_str(match disambiguated_data.data {
hir::definitions::DefPathData::Impl => "I", // Not specified in v0's <namespace>
hir::definitions::DefPathData::ForeignMod => "F", // Not specified in v0's <namespace>
hir::definitions::DefPathData::TypeNs(..) => "t",
hir::definitions::DefPathData::ValueNs(..) => "v",
hir::definitions::DefPathData::ClosureExpr => "C",
hir::definitions::DefPathData::Ctor => "c",
hir::definitions::DefPathData::AnonConst => "k",
hir::definitions::DefPathData::ImplTrait => "i",
hir::definitions::DefPathData::CrateRoot
| hir::definitions::DefPathData::Use
| hir::definitions::DefPathData::GlobalAsm
| hir::definitions::DefPathData::MacroNs(..)
| hir::definitions::DefPathData::LifetimeNs(..) => {
s.push(match disambiguated_data.data {
DefPathData::Impl => 'I', // Not specified in v0's <namespace>
DefPathData::ForeignMod => 'F', // Not specified in v0's <namespace>
DefPathData::TypeNs(..) => 't',
DefPathData::ValueNs(..) => 'v',
DefPathData::ClosureExpr(_generator_kind) => 'C',
DefPathData::Ctor => 'c',
DefPathData::AnonConst => 'k',
DefPathData::ImplTrait => 'i',
DefPathData::CrateRoot
| DefPathData::Use
| DefPathData::GlobalAsm
| DefPathData::MacroNs(..)
| DefPathData::LifetimeNs(..) => {
bug!("encode_ty_name: unexpected `{:?}`", disambiguated_data.data);
}
});
Expand Down
12 changes: 11 additions & 1 deletion compiler/rustc_symbol_mangling/src/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,17 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
// Uppercase categories are more stable than lowercase ones.
DefPathData::TypeNs(_) => 't',
DefPathData::ValueNs(_) => 'v',
DefPathData::ClosureExpr => 'C',
DefPathData::ClosureExpr(generator_kind) => {
// FIXME(swatinem): figure out how to move forward with closure-type aware mangling
// use rustc_hir::{AsyncGeneratorKind, GeneratorKind};
match generator_kind {
// Some(GeneratorKind::Async(AsyncGeneratorKind::Block)) => 'B',
// Some(GeneratorKind::Async(AsyncGeneratorKind::Closure)) => 'a',
// Some(GeneratorKind::Async(AsyncGeneratorKind::Fn)) => 'A',
// Some(GeneratorKind::Gen) => 'G',
_ => 'C',
}
}
DefPathData::Ctor => 'c',
DefPathData::AnonConst => 'k',
DefPathData::ImplTrait => 'i',
Expand Down