8266: Fix generic arguments being incorrectly offset in qualified trait casts r=flodiebold a=Veykril

We reverse the segments and generic args of the lowered path after building it, this wasn't accounted for when inserting the self parameter in `Type as Trait` segments.

Fixes #5886

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2021-03-31 09:52:47 +00:00 committed by GitHub
commit c69f6f31d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 1 deletions

View file

@ -74,6 +74,7 @@ pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path>
// <T as Trait<A>>::Foo desugars to Trait<Self=T, A>::Foo
Some(trait_ref) => {
let path = Path::from_src(trait_ref.path()?, hygiene)?;
let num_segments = path.mod_path.segments.len();
kind = path.mod_path.kind;
let mut prefix_segments = path.mod_path.segments;
@ -85,7 +86,8 @@ pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path>
generic_args.extend(prefix_args);
// Insert the type reference (T in the above example) as Self parameter for the trait
let last_segment = generic_args.last_mut()?;
let last_segment =
generic_args.iter_mut().rev().nth(num_segments.saturating_sub(1))?;
if last_segment.is_none() {
*last_segment = Some(Arc::new(GenericArgs::empty()));
};