Skip to content
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

Don't use unnormalized type in Ty::fn_sig call in rustdoc clean_middle_ty #102831

Merged
merged 1 commit into from
Oct 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1582,12 +1582,12 @@ fn normalize<'tcx>(cx: &mut DocContext<'tcx>, ty: Ty<'_>) -> Option<Ty<'tcx>> {
}

pub(crate) fn clean_middle_ty<'tcx>(
this: Ty<'tcx>,
ty: Ty<'tcx>,
Copy link
Member Author

Choose a reason for hiding this comment

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

Intentionally renaming this -> ty so that the normalized let ty = ... below shadows the unnormalized type, so this doesn't happen again.

cx: &mut DocContext<'tcx>,
def_id: Option<DefId>,
) -> Type {
trace!("cleaning type: {:?}", this);
let ty = normalize(cx, this).unwrap_or(this);
trace!("cleaning type: {:?}", ty);
let ty = normalize(cx, ty).unwrap_or(ty);
match *ty.kind() {
ty::Never => Primitive(PrimitiveType::Never),
ty::Bool => Primitive(PrimitiveType::Bool),
Expand All @@ -1610,7 +1610,6 @@ pub(crate) fn clean_middle_ty<'tcx>(
type_: Box::new(clean_middle_ty(ty, cx, None)),
},
ty::FnDef(..) | ty::FnPtr(_) => {
let ty = cx.tcx.lift(this).expect("FnPtr lift failed");
Copy link
Member Author

Choose a reason for hiding this comment

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

But technically this is the only fix needed -- this should've been cx.tcx.lift(ty), but also isn't needed at all.

let sig = ty.fn_sig(cx.tcx);
let decl = clean_fn_decl_from_did_and_sig(cx, None, sig);
BareFunction(Box::new(BareFunctionDecl {
Expand Down Expand Up @@ -1644,7 +1643,7 @@ pub(crate) fn clean_middle_ty<'tcx>(
let did = obj
.principal_def_id()
.or_else(|| dids.next())
.unwrap_or_else(|| panic!("found trait object `{:?}` with no traits?", this));
.unwrap_or_else(|| panic!("found trait object `{:?}` with no traits?", ty));
let substs = match obj.principal() {
Some(principal) => principal.skip_binder().substs,
// marker traits have no substs.
Expand Down
13 changes: 13 additions & 0 deletions src/test/rustdoc/normalize-assoc-item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,24 @@ impl Trait for usize {
type X = isize;
}

impl Trait for () {
type X = fn() -> i32;
}

impl Trait for isize {
type X = <() as Trait>::X;
}

// @has 'normalize_assoc_item/fn.f.html' '//pre[@class="rust fn"]' 'pub fn f() -> isize'
pub fn f() -> <usize as Trait>::X {
0
}

// @has 'normalize_assoc_item/fn.f2.html' '//pre[@class="rust fn"]' 'pub fn f2() -> fn() -> i32'
pub fn f2() -> <isize as Trait>::X {
todo!()
}

pub struct S {
// @has 'normalize_assoc_item/struct.S.html' '//span[@id="structfield.box_me_up"]' 'box_me_up: Box<S, Global>'
pub box_me_up: <S as Trait>::X,
Expand Down