]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/clean/inline.rs
Auto merge of #85363 - EFanZh:gdb-pretty-print-slices, r=michaelwoerister
[rust.git] / src / librustdoc / clean / inline.rs
1 //! Support for inlining external documentation into the current AST.
2
3 use std::iter::once;
4 use std::sync::Arc;
5
6 use rustc_ast as ast;
7 use rustc_data_structures::fx::FxHashSet;
8 use rustc_hir as hir;
9 use rustc_hir::def::{DefKind, Res};
10 use rustc_hir::def_id::DefId;
11 use rustc_hir::Mutability;
12 use rustc_metadata::creader::LoadedMacro;
13 use rustc_middle::ty::{self, TyCtxt};
14 use rustc_span::hygiene::MacroKind;
15 use rustc_span::symbol::{kw, sym, Symbol};
16
17 use crate::clean::{
18     self, utils, Attributes, AttributesExt, GetDefId, ItemId, NestedAttributesExt, Type,
19 };
20 use crate::core::DocContext;
21 use crate::formats::item_type::ItemType;
22
23 use super::{Clean, Visibility};
24
25 type Attrs<'hir> = rustc_middle::ty::Attributes<'hir>;
26
27 /// Attempt to inline a definition into this AST.
28 ///
29 /// This function will fetch the definition specified, and if it is
30 /// from another crate it will attempt to inline the documentation
31 /// from the other crate into this crate.
32 ///
33 /// This is primarily used for `pub use` statements which are, in general,
34 /// implementation details. Inlining the documentation should help provide a
35 /// better experience when reading the documentation in this use case.
36 ///
37 /// The returned value is `None` if the definition could not be inlined,
38 /// and `Some` of a vector of items if it was successfully expanded.
39 ///
40 /// `parent_module` refers to the parent of the *re-export*, not the original item.
41 crate fn try_inline(
42     cx: &mut DocContext<'_>,
43     parent_module: DefId,
44     res: Res,
45     name: Symbol,
46     attrs: Option<Attrs<'_>>,
47     visited: &mut FxHashSet<DefId>,
48 ) -> Option<Vec<clean::Item>> {
49     let did = res.opt_def_id()?;
50     if did.is_local() {
51         return None;
52     }
53     let mut ret = Vec::new();
54
55     debug!("attrs={:?}", attrs);
56     let attrs_clone = attrs;
57
58     let kind = match res {
59         Res::Def(DefKind::Trait, did) => {
60             record_extern_fqn(cx, did, ItemType::Trait);
61             build_impls(cx, Some(parent_module), did, attrs, &mut ret);
62             clean::TraitItem(build_external_trait(cx, did))
63         }
64         Res::Def(DefKind::Fn, did) => {
65             record_extern_fqn(cx, did, ItemType::Function);
66             clean::FunctionItem(build_external_function(cx, did))
67         }
68         Res::Def(DefKind::Struct, did) => {
69             record_extern_fqn(cx, did, ItemType::Struct);
70             build_impls(cx, Some(parent_module), did, attrs, &mut ret);
71             clean::StructItem(build_struct(cx, did))
72         }
73         Res::Def(DefKind::Union, did) => {
74             record_extern_fqn(cx, did, ItemType::Union);
75             build_impls(cx, Some(parent_module), did, attrs, &mut ret);
76             clean::UnionItem(build_union(cx, did))
77         }
78         Res::Def(DefKind::TyAlias, did) => {
79             record_extern_fqn(cx, did, ItemType::Typedef);
80             build_impls(cx, Some(parent_module), did, attrs, &mut ret);
81             clean::TypedefItem(build_type_alias(cx, did), false)
82         }
83         Res::Def(DefKind::Enum, did) => {
84             record_extern_fqn(cx, did, ItemType::Enum);
85             build_impls(cx, Some(parent_module), did, attrs, &mut ret);
86             clean::EnumItem(build_enum(cx, did))
87         }
88         Res::Def(DefKind::ForeignTy, did) => {
89             record_extern_fqn(cx, did, ItemType::ForeignType);
90             build_impls(cx, Some(parent_module), did, attrs, &mut ret);
91             clean::ForeignTypeItem
92         }
93         // Never inline enum variants but leave them shown as re-exports.
94         Res::Def(DefKind::Variant, _) => return None,
95         // Assume that enum variants and struct types are re-exported next to
96         // their constructors.
97         Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) => return Some(Vec::new()),
98         Res::Def(DefKind::Mod, did) => {
99             record_extern_fqn(cx, did, ItemType::Module);
100             clean::ModuleItem(build_module(cx, did, visited))
101         }
102         Res::Def(DefKind::Static, did) => {
103             record_extern_fqn(cx, did, ItemType::Static);
104             clean::StaticItem(build_static(cx, did, cx.tcx.is_mutable_static(did)))
105         }
106         Res::Def(DefKind::Const, did) => {
107             record_extern_fqn(cx, did, ItemType::Constant);
108             clean::ConstantItem(build_const(cx, did))
109         }
110         Res::Def(DefKind::Macro(kind), did) => {
111             let mac = build_macro(cx, did, name);
112
113             let type_kind = match kind {
114                 MacroKind::Bang => ItemType::Macro,
115                 MacroKind::Attr => ItemType::ProcAttribute,
116                 MacroKind::Derive => ItemType::ProcDerive,
117             };
118             record_extern_fqn(cx, did, type_kind);
119             mac
120         }
121         _ => return None,
122     };
123
124     let (attrs, cfg) = merge_attrs(cx, Some(parent_module), load_attrs(cx, did), attrs_clone);
125     cx.inlined.insert(did.into());
126     ret.push(clean::Item::from_def_id_and_attrs_and_parts(
127         did,
128         Some(name),
129         kind,
130         box attrs,
131         cx,
132         cfg,
133     ));
134     Some(ret)
135 }
136
137 crate fn try_inline_glob(
138     cx: &mut DocContext<'_>,
139     res: Res,
140     visited: &mut FxHashSet<DefId>,
141 ) -> Option<Vec<clean::Item>> {
142     let did = res.opt_def_id()?;
143     if did.is_local() {
144         return None;
145     }
146
147     match res {
148         Res::Def(DefKind::Mod, did) => {
149             let m = build_module(cx, did, visited);
150             Some(m.items)
151         }
152         // glob imports on things like enums aren't inlined even for local exports, so just bail
153         _ => None,
154     }
155 }
156
157 crate fn load_attrs<'hir>(cx: &DocContext<'hir>, did: DefId) -> Attrs<'hir> {
158     cx.tcx.get_attrs(did)
159 }
160
161 /// Record an external fully qualified name in the external_paths cache.
162 ///
163 /// These names are used later on by HTML rendering to generate things like
164 /// source links back to the original item.
165 crate fn record_extern_fqn(cx: &mut DocContext<'_>, did: DefId, kind: ItemType) {
166     let crate_name = cx.tcx.crate_name(did.krate).to_string();
167
168     let relative = cx.tcx.def_path(did).data.into_iter().filter_map(|elem| {
169         // extern blocks have an empty name
170         let s = elem.data.to_string();
171         if !s.is_empty() { Some(s) } else { None }
172     });
173     let fqn = if let ItemType::Macro = kind {
174         // Check to see if it is a macro 2.0 or built-in macro
175         if matches!(
176             cx.enter_resolver(|r| r.cstore().load_macro_untracked(did, cx.sess())),
177             LoadedMacro::MacroDef(def, _)
178                 if matches!(&def.kind, ast::ItemKind::MacroDef(ast_def)
179                     if !ast_def.macro_rules)
180         ) {
181             once(crate_name).chain(relative).collect()
182         } else {
183             vec![crate_name, relative.last().expect("relative was empty")]
184         }
185     } else {
186         once(crate_name).chain(relative).collect()
187     };
188
189     if did.is_local() {
190         cx.cache.exact_paths.insert(did, fqn);
191     } else {
192         cx.cache.external_paths.insert(did, (fqn, kind));
193     }
194 }
195
196 crate fn build_external_trait(cx: &mut DocContext<'_>, did: DefId) -> clean::Trait {
197     let trait_items = cx
198         .tcx
199         .associated_items(did)
200         .in_definition_order()
201         .map(|item| {
202             // When building an external trait, the cleaned trait will have all items public,
203             // which causes methods to have a `pub` prefix, which is invalid since items in traits
204             // can not have a visibility prefix. Thus we override the visibility here manually.
205             // See https://github.com/rust-lang/rust/issues/81274
206             clean::Item { visibility: Visibility::Inherited, ..item.clean(cx) }
207         })
208         .collect();
209
210     let predicates = cx.tcx.predicates_of(did);
211     let generics = (cx.tcx.generics_of(did), predicates).clean(cx);
212     let generics = filter_non_trait_generics(did, generics);
213     let (generics, supertrait_bounds) = separate_supertrait_bounds(generics);
214     let is_auto = cx.tcx.trait_is_auto(did);
215     clean::Trait {
216         unsafety: cx.tcx.trait_def(did).unsafety,
217         generics,
218         items: trait_items,
219         bounds: supertrait_bounds,
220         is_auto,
221     }
222 }
223
224 fn build_external_function(cx: &mut DocContext<'_>, did: DefId) -> clean::Function {
225     let sig = cx.tcx.fn_sig(did);
226
227     let constness =
228         if cx.tcx.is_const_fn_raw(did) { hir::Constness::Const } else { hir::Constness::NotConst };
229     let asyncness = cx.tcx.asyncness(did);
230     let predicates = cx.tcx.predicates_of(did);
231     let (generics, decl) = clean::enter_impl_trait(cx, |cx| {
232         ((cx.tcx.generics_of(did), predicates).clean(cx), (did, sig).clean(cx))
233     });
234     clean::Function {
235         decl,
236         generics,
237         header: hir::FnHeader { unsafety: sig.unsafety(), abi: sig.abi(), constness, asyncness },
238     }
239 }
240
241 fn build_enum(cx: &mut DocContext<'_>, did: DefId) -> clean::Enum {
242     let predicates = cx.tcx.explicit_predicates_of(did);
243
244     clean::Enum {
245         generics: (cx.tcx.generics_of(did), predicates).clean(cx),
246         variants_stripped: false,
247         variants: cx.tcx.adt_def(did).variants.clean(cx),
248     }
249 }
250
251 fn build_struct(cx: &mut DocContext<'_>, did: DefId) -> clean::Struct {
252     let predicates = cx.tcx.explicit_predicates_of(did);
253     let variant = cx.tcx.adt_def(did).non_enum_variant();
254
255     clean::Struct {
256         struct_type: variant.ctor_kind,
257         generics: (cx.tcx.generics_of(did), predicates).clean(cx),
258         fields: variant.fields.clean(cx),
259         fields_stripped: false,
260     }
261 }
262
263 fn build_union(cx: &mut DocContext<'_>, did: DefId) -> clean::Union {
264     let predicates = cx.tcx.explicit_predicates_of(did);
265     let variant = cx.tcx.adt_def(did).non_enum_variant();
266
267     clean::Union {
268         generics: (cx.tcx.generics_of(did), predicates).clean(cx),
269         fields: variant.fields.clean(cx),
270         fields_stripped: false,
271     }
272 }
273
274 fn build_type_alias(cx: &mut DocContext<'_>, did: DefId) -> clean::Typedef {
275     let predicates = cx.tcx.explicit_predicates_of(did);
276     let type_ = cx.tcx.type_of(did).clean(cx);
277
278     clean::Typedef {
279         type_,
280         generics: (cx.tcx.generics_of(did), predicates).clean(cx),
281         item_type: None,
282     }
283 }
284
285 /// Builds all inherent implementations of an ADT (struct/union/enum) or Trait item/path/reexport.
286 crate fn build_impls(
287     cx: &mut DocContext<'_>,
288     parent_module: Option<DefId>,
289     did: DefId,
290     attrs: Option<Attrs<'_>>,
291     ret: &mut Vec<clean::Item>,
292 ) {
293     let tcx = cx.tcx;
294
295     // for each implementation of an item represented by `did`, build the clean::Item for that impl
296     for &did in tcx.inherent_impls(did).iter() {
297         build_impl(cx, parent_module, did, attrs, ret);
298     }
299 }
300
301 /// `parent_module` refers to the parent of the re-export, not the original item
302 fn merge_attrs(
303     cx: &mut DocContext<'_>,
304     parent_module: Option<DefId>,
305     old_attrs: Attrs<'_>,
306     new_attrs: Option<Attrs<'_>>,
307 ) -> (clean::Attributes, Option<Arc<clean::cfg::Cfg>>) {
308     // NOTE: If we have additional attributes (from a re-export),
309     // always insert them first. This ensure that re-export
310     // doc comments show up before the original doc comments
311     // when we render them.
312     if let Some(inner) = new_attrs {
313         let mut both = inner.to_vec();
314         both.extend_from_slice(old_attrs);
315         (
316             if let Some(new_id) = parent_module {
317                 Attributes::from_ast(old_attrs, Some((inner, new_id)))
318             } else {
319                 Attributes::from_ast(&both, None)
320             },
321             both.cfg(cx.sess()),
322         )
323     } else {
324         (old_attrs.clean(cx), old_attrs.cfg(cx.sess()))
325     }
326 }
327
328 /// Builds a specific implementation of a type. The `did` could be a type method or trait method.
329 crate fn build_impl(
330     cx: &mut DocContext<'_>,
331     parent_module: impl Into<Option<DefId>>,
332     did: DefId,
333     attrs: Option<Attrs<'_>>,
334     ret: &mut Vec<clean::Item>,
335 ) {
336     if !cx.inlined.insert(did.into()) {
337         return;
338     }
339
340     let tcx = cx.tcx;
341     let associated_trait = tcx.impl_trait_ref(did);
342
343     // Only inline impl if the implemented trait is
344     // reachable in rustdoc generated documentation
345     if !did.is_local() {
346         if let Some(traitref) = associated_trait {
347             let did = traitref.def_id;
348             if !cx.cache.access_levels.is_public(did) {
349                 return;
350             }
351
352             if let Some(stab) = tcx.lookup_stability(did) {
353                 if stab.level.is_unstable() && stab.feature == sym::rustc_private {
354                     return;
355                 }
356             }
357         }
358     }
359
360     let impl_item = match did.as_local() {
361         Some(did) => {
362             let hir_id = tcx.hir().local_def_id_to_hir_id(did);
363             match &tcx.hir().expect_item(hir_id).kind {
364                 hir::ItemKind::Impl(impl_) => Some(impl_),
365                 _ => panic!("`DefID` passed to `build_impl` is not an `impl"),
366             }
367         }
368         None => None,
369     };
370
371     let for_ = match &impl_item {
372         Some(impl_) => impl_.self_ty.clean(cx),
373         None => tcx.type_of(did).clean(cx),
374     };
375
376     // Only inline impl if the implementing type is
377     // reachable in rustdoc generated documentation
378     if !did.is_local() {
379         if let Some(did) = for_.def_id() {
380             if !cx.cache.access_levels.is_public(did) {
381                 return;
382             }
383
384             if let Some(stab) = tcx.lookup_stability(did) {
385                 if stab.level.is_unstable() && stab.feature == sym::rustc_private {
386                     return;
387                 }
388             }
389         }
390     }
391
392     let predicates = tcx.explicit_predicates_of(did);
393     let (trait_items, generics) = match impl_item {
394         Some(impl_) => (
395             impl_
396                 .items
397                 .iter()
398                 .map(|item| tcx.hir().impl_item(item.id).clean(cx))
399                 .collect::<Vec<_>>(),
400             impl_.generics.clean(cx),
401         ),
402         None => (
403             tcx.associated_items(did)
404                 .in_definition_order()
405                 .filter_map(|item| {
406                     if associated_trait.is_some() || item.vis == ty::Visibility::Public {
407                         Some(item.clean(cx))
408                     } else {
409                         None
410                     }
411                 })
412                 .collect::<Vec<_>>(),
413             clean::enter_impl_trait(cx, |cx| (tcx.generics_of(did), predicates).clean(cx)),
414         ),
415     };
416     let polarity = tcx.impl_polarity(did);
417     let trait_ = associated_trait.clean(cx).map(|bound| match bound {
418         clean::GenericBound::TraitBound(polyt, _) => polyt.trait_,
419         clean::GenericBound::Outlives(..) => unreachable!(),
420     });
421     if trait_.def_id() == tcx.lang_items().deref_trait() {
422         super::build_deref_target_impls(cx, &trait_items, ret);
423     }
424
425     // Return if the trait itself or any types of the generic parameters are doc(hidden).
426     let mut stack: Vec<&Type> = trait_.iter().collect();
427     stack.push(&for_);
428     while let Some(ty) = stack.pop() {
429         if let Some(did) = ty.def_id() {
430             if cx.tcx.get_attrs(did).lists(sym::doc).has_word(sym::hidden) {
431                 return;
432             }
433         }
434         if let Some(generics) = ty.generics() {
435             stack.extend(generics);
436         }
437     }
438
439     if let Some(trait_did) = trait_.def_id() {
440         record_extern_trait(cx, trait_did);
441     }
442
443     let (merged_attrs, cfg) = merge_attrs(cx, parent_module.into(), load_attrs(cx, did), attrs);
444     debug!("merged_attrs={:?}", merged_attrs);
445
446     debug!("build_impl: impl {:?} for {:?}", trait_.def_id(), for_.def_id());
447     ret.push(clean::Item::from_def_id_and_attrs_and_parts(
448         did,
449         None,
450         clean::ImplItem(clean::Impl {
451             span: clean::types::rustc_span(did, cx.tcx),
452             unsafety: hir::Unsafety::Normal,
453             generics,
454             trait_,
455             for_,
456             items: trait_items,
457             negative_polarity: polarity.clean(cx),
458             synthetic: false,
459             blanket_impl: None,
460         }),
461         box merged_attrs,
462         cx,
463         cfg,
464     ));
465 }
466
467 fn build_module(
468     cx: &mut DocContext<'_>,
469     did: DefId,
470     visited: &mut FxHashSet<DefId>,
471 ) -> clean::Module {
472     let mut items = Vec::new();
473
474     // If we're re-exporting a re-export it may actually re-export something in
475     // two namespaces, so the target may be listed twice. Make sure we only
476     // visit each node at most once.
477     for &item in cx.tcx.item_children(did).iter() {
478         if item.vis == ty::Visibility::Public {
479             if let Some(def_id) = item.res.mod_def_id() {
480                 if did == def_id || !visited.insert(def_id) {
481                     continue;
482                 }
483             }
484             if let Res::PrimTy(p) = item.res {
485                 // Primitive types can't be inlined so generate an import instead.
486                 let prim_ty = clean::PrimitiveType::from(p);
487                 items.push(clean::Item {
488                     name: None,
489                     attrs: box clean::Attributes::default(),
490                     def_id: ItemId::Primitive(prim_ty, did.krate),
491                     visibility: clean::Public,
492                     kind: box clean::ImportItem(clean::Import::new_simple(
493                         item.ident.name,
494                         clean::ImportSource {
495                             path: clean::Path {
496                                 global: false,
497                                 res: item.res,
498                                 segments: vec![clean::PathSegment {
499                                     name: prim_ty.as_sym(),
500                                     args: clean::GenericArgs::AngleBracketed {
501                                         args: Vec::new(),
502                                         bindings: Vec::new(),
503                                     },
504                                 }],
505                             },
506                             did: None,
507                         },
508                         true,
509                     )),
510                     cfg: None,
511                 });
512             } else if let Some(i) = try_inline(cx, did, item.res, item.ident.name, None, visited) {
513                 items.extend(i)
514             }
515         }
516     }
517
518     let span = clean::Span::from_rustc_span(cx.tcx.def_span(did));
519     clean::Module { items, span }
520 }
521
522 crate fn print_inlined_const(tcx: TyCtxt<'_>, did: DefId) -> String {
523     if let Some(did) = did.as_local() {
524         let hir_id = tcx.hir().local_def_id_to_hir_id(did);
525         rustc_hir_pretty::id_to_string(&tcx.hir(), hir_id)
526     } else {
527         tcx.rendered_const(did)
528     }
529 }
530
531 fn build_const(cx: &mut DocContext<'_>, def_id: DefId) -> clean::Constant {
532     clean::Constant {
533         type_: cx.tcx.type_of(def_id).clean(cx),
534         kind: clean::ConstantKind::Extern { def_id },
535     }
536 }
537
538 fn build_static(cx: &mut DocContext<'_>, did: DefId, mutable: bool) -> clean::Static {
539     clean::Static {
540         type_: cx.tcx.type_of(did).clean(cx),
541         mutability: if mutable { Mutability::Mut } else { Mutability::Not },
542         expr: None,
543     }
544 }
545
546 fn build_macro(cx: &mut DocContext<'_>, did: DefId, name: Symbol) -> clean::ItemKind {
547     let imported_from = cx.tcx.crate_name(did.krate);
548     match cx.enter_resolver(|r| r.cstore().load_macro_untracked(did, cx.sess())) {
549         LoadedMacro::MacroDef(def, _) => {
550             if let ast::ItemKind::MacroDef(ref def) = def.kind {
551                 let tts: Vec<_> = def.body.inner_tokens().into_trees().collect();
552                 let matchers = tts.chunks(4).map(|arm| &arm[0]);
553
554                 let source = format!(
555                     "macro_rules! {} {{\n{}}}",
556                     name,
557                     utils::render_macro_arms(matchers, ";")
558                 );
559
560                 clean::MacroItem(clean::Macro { source, imported_from: Some(imported_from) })
561             } else {
562                 unreachable!()
563             }
564         }
565         LoadedMacro::ProcMacro(ext) => clean::ProcMacroItem(clean::ProcMacro {
566             kind: ext.macro_kind(),
567             helpers: ext.helper_attrs,
568         }),
569     }
570 }
571
572 /// A trait's generics clause actually contains all of the predicates for all of
573 /// its associated types as well. We specifically move these clauses to the
574 /// associated types instead when displaying, so when we're generating the
575 /// generics for the trait itself we need to be sure to remove them.
576 /// We also need to remove the implied "recursive" Self: Trait bound.
577 ///
578 /// The inverse of this filtering logic can be found in the `Clean`
579 /// implementation for `AssociatedType`
580 fn filter_non_trait_generics(trait_did: DefId, mut g: clean::Generics) -> clean::Generics {
581     for pred in &mut g.where_predicates {
582         match *pred {
583             clean::WherePredicate::BoundPredicate {
584                 ty: clean::Generic(ref s),
585                 ref mut bounds,
586                 ..
587             } if *s == kw::SelfUpper => {
588                 bounds.retain(|bound| match *bound {
589                     clean::GenericBound::TraitBound(
590                         clean::PolyTrait { trait_: clean::ResolvedPath { did, .. }, .. },
591                         _,
592                     ) => did != trait_did,
593                     _ => true,
594                 });
595             }
596             _ => {}
597         }
598     }
599
600     g.where_predicates.retain(|pred| match *pred {
601         clean::WherePredicate::BoundPredicate {
602             ty:
603                 clean::QPath {
604                     self_type: box clean::Generic(ref s),
605                     trait_: box clean::ResolvedPath { did, .. },
606                     name: ref _name,
607                     ..
608                 },
609             ref bounds,
610             ..
611         } => !(bounds.is_empty() || *s == kw::SelfUpper && did == trait_did),
612         _ => true,
613     });
614     g
615 }
616
617 /// Supertrait bounds for a trait are also listed in the generics coming from
618 /// the metadata for a crate, so we want to separate those out and create a new
619 /// list of explicit supertrait bounds to render nicely.
620 fn separate_supertrait_bounds(
621     mut g: clean::Generics,
622 ) -> (clean::Generics, Vec<clean::GenericBound>) {
623     let mut ty_bounds = Vec::new();
624     g.where_predicates.retain(|pred| match *pred {
625         clean::WherePredicate::BoundPredicate { ty: clean::Generic(ref s), ref bounds, .. }
626             if *s == kw::SelfUpper =>
627         {
628             ty_bounds.extend(bounds.iter().cloned());
629             false
630         }
631         _ => true,
632     });
633     (g, ty_bounds)
634 }
635
636 crate fn record_extern_trait(cx: &mut DocContext<'_>, did: DefId) {
637     if did.is_local() {
638         return;
639     }
640
641     {
642         if cx.external_traits.borrow().contains_key(&did) || cx.active_extern_traits.contains(&did)
643         {
644             return;
645         }
646     }
647
648     {
649         cx.active_extern_traits.insert(did);
650     }
651
652     debug!("record_extern_trait: {:?}", did);
653     let trait_ = build_external_trait(cx, did);
654
655     let trait_ = clean::TraitWithExtraInfo {
656         trait_,
657         is_notable: clean::utils::has_doc_flag(cx.tcx.get_attrs(did), sym::notable_trait),
658     };
659     cx.external_traits.borrow_mut().insert(did, trait_);
660     cx.active_extern_traits.remove(&did);
661 }