Skip to content

Conversation

@JelleZijlstra
Copy link
Contributor

@JelleZijlstra JelleZijlstra commented Aug 27, 2025

This is a variant of #20076 that moves some complexity out of apply_type_mapping_impl in generics.rs. The tradeoff is that now every place that applies TypeMapping::Specialization must take care to call .materialize() afterwards. (A previous version of this didn't work because I had missed a spot where I had to call .materialize().)

@carljm as asked in #20076 (comment) .

@github-actions
Copy link
Contributor

github-actions bot commented Aug 27, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Aug 27, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Aug 27, 2025

mypy_primer results

No ecosystem changes detected ✅
No memory usage changes detected ✅

@JelleZijlstra JelleZijlstra changed the title [ty] Variant of #20076 (simpler materialization of specialized generics) [ty] Simplify materialization of specialized generics Aug 28, 2025
@JelleZijlstra JelleZijlstra marked this pull request as ready for review August 28, 2025 14:40
@AlexWaygood AlexWaygood removed their request for review August 28, 2025 14:44
Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

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

This seems like a win to me. The materialization kind is part of the specialization; it seems reasonable that apply_specialization methods would be responsible for handling it.

I think if we wanted to invest in better error-resilience here (if we find ourselves forgetting to apply the materialization in more cases), the approach would be to extract a Rust type that holds only the types part of a Specialization, and have TypeMappling::Specialization hold only that Rust type, so you'd have to extract that and the materialization from a Specialization in order to wrap it in a TypeMapping. This would make it impossible to ignore the materialization part accidentally.

) -> Self {
if let Some(specialization) = specialization {
self.apply_type_mapping_impl(
let new_self = self.apply_type_mapping_impl(
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice find!

@carljm carljm merged commit 3927b0c into astral-sh:main Aug 28, 2025
38 checks passed
@JelleZijlstra JelleZijlstra deleted the matrel2 branch August 29, 2025 13:09
second-ed pushed a commit to second-ed/ruff that referenced this pull request Sep 9, 2025
This is a variant of astral-sh#20076 that moves some complexity out of
`apply_type_mapping_impl` in `generics.rs`. The tradeoff is that now
every place that applies `TypeMapping::Specialization` must take care to
call `.materialize()` afterwards. (A previous version of this didn't
work because I had missed a spot where I had to call `.materialize()`.)

@carljm as asked in
astral-sh#20076 (comment) .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants