Repository navigation
reflection: doc attributes from adts, fields, and variants #165094
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,8 +4,8 @@ use std::{fmt, mem}; | |
|
|
||
| use rustc_abi::{Align, FIRST_VARIANT, FieldIdx, Size, VariantIdx}; | ||
| use rustc_ast::Mutability; | ||
| use rustc_attr_ir::find_attr; | ||
| use rustc_attr_ir::lang_items::LangItem; | ||
| use rustc_attr_ir::{HasAttrs, find_attr}; | ||
| use rustc_data_structures::fx::{FxHashMap, FxIndexMap, IndexEntry}; | ||
| use rustc_hir::def_id::{DefId, LocalDefId}; | ||
| use rustc_hir::{CRATE_HIR_ID, HirId}; | ||
|
|
@@ -796,6 +796,54 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> { | |
| )?; | ||
| } | ||
|
|
||
| sym::type_id_docs => { | ||
| let ty = ecx.read_type_id(&args[0])?; | ||
| let attrs = match ty.kind() { | ||
| ty::Adt(def, args) | ||
| if let Some(FieldInfo { base, variant_idx, field_idx, .. }) = | ||
| def.field_representing_type_info(ecx.tcx(), *args) => | ||
| { | ||
| match base.kind() { | ||
| ty::Adt(base_def, _) => { | ||
| let field = &base_def.variants()[variant_idx].fields[field_idx]; | ||
| field.did.get_attrs(&ecx.tcx) | ||
| } | ||
| ty::Tuple(_) => &[], | ||
| _ => { | ||
| // only adts and tuples have fields | ||
| span_bug!(ecx.cur_span(), "expected adt or tuple type, got {ty}"); | ||
| } | ||
| } | ||
| } | ||
| ty::Adt(def, _) => def.did().get_attrs(&ecx.tcx), | ||
| _ => &[], | ||
| }; | ||
|
|
||
| ecx.write_doc_attributes(dest, attrs)?; | ||
| } | ||
|
|
||
| sym::type_id_variant_docs => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should add a variant-pattern type (similar to what we do with FRTs instead of also having a field variant of many intrinsics) sooner rather than later instead of having to have all these intrinsics duplicated for variants and for normal types. Not for this PR, but just adding them to the type system without exposing them at the AST level shouldn't be too hard (famous last words) |
||
| let base = ecx.read_type_id(&args[0])?; | ||
|
|
||
| if let ty::Adt(def, _) = base.kind() { | ||
| let variant_idx = ecx.read_target_usize(&args[1])? as usize; | ||
| if variant_idx >= def.variants().len() { | ||
| throw_ub!(BoundsCheckFailed { | ||
| len: def.variants().len() as u64, | ||
| index: variant_idx as u64 | ||
| }); | ||
| } | ||
| let variant_idx = VariantIdx::from_usize(variant_idx); | ||
|
|
||
| ecx.write_doc_attributes( | ||
| dest, | ||
| def.variant(variant_idx).def_id.get_attrs(&ecx.tcx), | ||
| )?; | ||
| } else { | ||
| span_bug!(ecx.cur_span(), "expected enum type, got {base}") | ||
| }; | ||
| } | ||
|
|
||
| sym::type_id_variant_non_exhaustive => { | ||
| let base = ecx.read_type_id(&args[0])?; | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The list of intrinsics here is growing so much that I think it'd make sense to move it to a separate file. (In a separate PR.)
View changes since the review