Skip to content

Commit a0c239f

Browse files
ShaddyDCdingxiangfei2009
authored andcommitted
implement builtin # raw_handle and place handles
Co-authored-by: Xiangfei Ding <dingxiangfei2009protonmail.ch> Signed-off-by: Rasmus Piorr <piorr@google.com>
1 parent d74b55f commit a0c239f

41 files changed

Lines changed: 978 additions & 13 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎compiler/rustc_ast/src/ast.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,6 +1616,7 @@ impl Expr {
16161616
| ExprKind::Match(..)
16171617
| ExprKind::MethodCall(..)
16181618
| ExprKind::OffsetOf(..)
1619+
| ExprKind::RawHandle(..)
16191620
| ExprKind::Paren(..)
16201621
| ExprKind::Path(..)
16211622
| ExprKind::Repeat(..)
@@ -1875,6 +1876,9 @@ pub enum ExprKind {
18751876
/// indirectly via the macro `core::mem::offset_of!(...)`.
18761877
OffsetOf(Box<Ty>, ThinVec<Ident>),
18771878

1879+
/// A `raw_handle` expression (e.g. `builtin # raw_handle(expr)`).
1880+
RawHandle(Box<Expr>),
1881+
18781882
/// A macro invocation; pre-expansion.
18791883
MacCall(Box<MacCall>),
18801884

‎compiler/rustc_ast/src/util/classify.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ pub fn leading_labeled_expr(mut expr: &ast::Expr) -> bool {
144144
| MacCall(..)
145145
| Match(_, _, MatchKind::Prefix)
146146
| OffsetOf(..)
147+
| RawHandle(..)
147148
| Paren(..)
148149
| Path(..)
149150
| Range(None, _, _)
@@ -218,7 +219,7 @@ pub fn expr_trailing_brace(mut expr: &ast::Expr) -> Option<TrailingBrace<'_>> {
218219
break (mac.args.delim == Delimiter::Brace).then_some(TrailingBrace::MacCall(mac));
219220
}
220221

221-
InlineAsm(_) | OffsetOf(_, _) | IncludedBytes(_) | FormatArgs(_) => {
222+
InlineAsm(_) | OffsetOf(_, _) | RawHandle(_) | IncludedBytes(_) | FormatArgs(_) => {
222223
// These should have been denied pre-expansion.
223224
break None;
224225
}

‎compiler/rustc_ast/src/visit.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,8 @@ macro_rules! common_visitor_and_walkers {
10991099
visit_visitable!(vis, f),
11001100
ExprKind::OffsetOf(container, fields) =>
11011101
visit_visitable!(vis, container, fields),
1102+
ExprKind::RawHandle(subexpression) =>
1103+
visit_visitable!(vis, subexpression),
11021104
ExprKind::Yield(kind) =>
11031105
visit_visitable!(vis, kind),
11041106
ExprKind::Try(subexpression) =>

‎compiler/rustc_ast_lowering/src/expr.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
500500
),
501501
self.arena.alloc_from_iter(fields.iter().map(|&ident| self.lower_ident(ident))),
502502
),
503+
ExprKind::RawHandle(expr) => hir::ExprKind::RawHandle(self.lower_expr(expr)),
503504
ExprKind::Struct(se) => {
504505
let rest = match se.rest {
505506
StructRest::Base(ref e) => hir::StructTailExpr::Base(self.lower_expr(e)),

‎compiler/rustc_ast_pretty/src/pprust/state/expr.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,14 @@ impl<'a> State<'a> {
813813
self.end(ib);
814814
self.pclose();
815815
}
816+
ast::ExprKind::RawHandle(expr) => {
817+
self.word("builtin # raw_handle");
818+
self.popen();
819+
let ib = self.ibox(0);
820+
self.print_expr(expr, FixupContext::default());
821+
self.end(ib);
822+
self.pclose();
823+
}
816824
ast::ExprKind::MacCall(m) => self.print_mac(m),
817825
ast::ExprKind::Paren(e) => {
818826
self.popen();

‎compiler/rustc_attr_ir/src/lang_items.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,15 @@ language_item_table! {
464464
FieldType, sym::field_type, field_type, Target::AssocTy(AssocCtxt::Trait), GenericRequirement::Exact(0);
465465
FieldOffset, sym::field_offset, field_offset, Target::AssocConst(AssocCtxt::Trait), GenericRequirement::Exact(0);
466466

467+
// Place handles.
468+
LocalHandle, sym::local_handle, local_handle, Target::Struct, GenericRequirement::Exact(1);
469+
DerefPlace, sym::deref_place, deref_place_trait, Target::Trait, GenericRequirement::Exact(0);
470+
DerefPlacePointeeHandle, sym::deref_place_pointee_handle, deref_place_pointee_handle, Target::AssocTy(AssocCtxt::Trait), GenericRequirement::None;
471+
DerefPlaceDerefPlace, sym::deref_place_deref_place, deref_place_deref_place, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
472+
ProjectPlace, sym::project_place, project_place_trait, Target::Trait, GenericRequirement::Exact(1);
473+
ProjectPlaceProjected, sym::project_place_projected, project_place_projected, Target::AssocTy(AssocCtxt::Trait), GenericRequirement::None;
474+
ProjectPlaceProjectPlace, sym::project_place_project_place, project_place_project_place, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
475+
467476
// Used to fallback `{float}` to `f32` when `f32: From<{float}>`
468477
From, sym::From, from_trait, Target::Trait, GenericRequirement::Exact(1);
469478
FromFn, sym::from, from_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;

‎compiler/rustc_builtin_macros/src/assert/context.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
311311
| ExprKind::Loop(_, _, _)
312312
| ExprKind::MacCall(_)
313313
| ExprKind::OffsetOf(_, _)
314+
| ExprKind::RawHandle(..)
314315
| ExprKind::Path(_, _)
315316
| ExprKind::Ret(_)
316317
| ExprKind::Try(_)

‎compiler/rustc_hir/src/hir.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2273,6 +2273,7 @@ impl Expr<'_> {
22732273
| ExprKind::Match(..)
22742274
| ExprKind::MethodCall(..)
22752275
| ExprKind::OffsetOf(..)
2276+
| ExprKind::RawHandle(..)
22762277
| ExprKind::Path(..)
22772278
| ExprKind::Repeat(..)
22782279
| ExprKind::Struct(..)
@@ -2345,6 +2346,7 @@ impl Expr<'_> {
23452346
| ExprKind::Assign(..)
23462347
| ExprKind::InlineAsm(..)
23472348
| ExprKind::OffsetOf(..)
2349+
| ExprKind::RawHandle(..)
23482350
| ExprKind::AssignOp(..)
23492351
| ExprKind::Lit(_)
23502352
| ExprKind::ConstBlock(..)
@@ -2415,6 +2417,7 @@ impl Expr<'_> {
24152417
| ExprKind::Index(base, _, _)
24162418
| ExprKind::AddrOf(.., base)
24172419
| ExprKind::Cast(base, _)
2420+
| ExprKind::RawHandle(base)
24182421
| ExprKind::UnsafeBinderCast(_, base, _) => {
24192422
// This isn't exactly true for `Index` and all `Unary`, but we are using this
24202423
// method exclusively for diagnostics and there's a *cultural* pressure against
@@ -2690,6 +2693,9 @@ pub enum ExprKind<'hir> {
26902693
/// Field offset (`offset_of!`)
26912694
OffsetOf(&'hir Ty<'hir>, &'hir [Ident]),
26922695

2696+
/// Raw place handle (`raw_handle!`)
2697+
RawHandle(&'hir Expr<'hir>),
2698+
26932699
/// A struct or struct-like variant literal expression.
26942700
///
26952701
/// E.g., `Foo {x: 1, y: 2}`, or `Foo {x: 1, .. base}`,

‎compiler/rustc_hir/src/intravisit.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,9 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
958958
try_visit!(visitor.visit_ty_unambig(container));
959959
walk_list!(visitor, visit_ident, fields.iter().copied());
960960
}
961+
ExprKind::RawHandle(ref subexpression) => {
962+
try_visit!(visitor.visit_expr(subexpression));
963+
}
961964
ExprKind::Yield(ref subexpression, _) => {
962965
try_visit!(visitor.visit_expr(subexpression));
963966
}

‎compiler/rustc_hir_pretty/src/lib.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,6 +1783,11 @@ impl<'a> State<'a> {
17831783

17841784
self.word(")");
17851785
}
1786+
hir::ExprKind::RawHandle(expr) => {
1787+
self.word("raw_handle!(");
1788+
self.print_expr(expr);
1789+
self.word(")");
1790+
}
17861791
hir::ExprKind::UnsafeBinderCast(kind, expr, ty) => {
17871792
match kind {
17881793
ast::UnsafeBinderCastKind::Wrap => self.word("wrap_binder!("),

0 commit comments

Comments
 (0)