changed 3 years ago
Linked with GitHub

polymorphization

change tcx.unused_generic_parameters to be as permissive as possible,
pretty much only rejecting stuff needed for Layout (for now) and type_id. Rename it to used_generic_parameters probably.

Then, this should only require changes to mono item collection.

Pseudocode, figure out how this has to work:

// The actual method name, the rest is fake. fn collect_items_rec(item, concrete_substs) -> MonoItem { // probably just iterating all mono items for the `item`. // // will need to change `visited` to some sort of nested collection. if let Some(item) = already_monomorphized(item, concrete_substs) { return item; } else in_cycle(item, concrete_substs) { // not sure, maybe return just a fully concrete version for now? // // that's definitely sound, but a bit unfortunate. return TODO; } let used_substs = tcx.used_generic_parameters(item); let mut maximal_substs = concrete_substs .iter() .enumerate() .map(|(i, arg)| if used_substs.get(i) { arg } else { ty::Param } ).collect(); for (item, substs) in reachable_stuff { let concrete_item_substs = substs.subst(concrete_substs); let mono_item = collect_items_rec(item, concrete_item_substs); let needed_substs = mono_item.substs; // in a fresh `InferCtxt`: let needed_substs_infer = needed_substs.substs(fresh_infer_args); let mut max_substs_infer = maximal_substs.subst(fresh_infer_args); let max_item_substs = substs.subst(maximal_substs_infer); equate(needed_substs, max_item_substs); // This should never fail. // We might need to introduce a new `TyKind` for this to make // sense instead of using `ty::Param`. maximal_substs = convert_infer_to_fresh_param(max_substs_infer); } let mono_item = (item, maximal_substs); // ...insert into `visited` and remove from cycle detection mono_item }

Difficulties

We have to somehow remember substs for each mono item. Probably
change Neighbors to either

  • store the MonoItem without eagerly substituting (seems difficult, as we need to resolve instances).
  • (generic_substs, mono_item) pairs where generic_substs can start out as an option with us failing polymorphization if it is None for at least one neighbour.

Where is polymorphization used

  • codegen for functions: need to keep polymorphization info around around
    • how? add it to the FunctionCx?
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
  • Instance::polymorphize. Maybe return the polymorphization results from a query?
    • could use a more complex data structure instead of DefIdSet in collect_and_partition_mono_items. Polymorphize would then "simply fetch" the polymorphized instance which applies to the concrete instance
Select a repo