rust-lang / rust-lang/rust

`type_info` on higher-ranked function pointer produces `TypeId` with strange lifetime.

Open
#153,176 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-higher-ranked C-bug F-type_info T-compiler T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

In the following code, all the assertions pass. The out variable is set to the TypeId of the return type of for<'a> fn(&'a ()) -> &'a (). I don't know what the lifetime of this reference type is. The type seems to have the following properties:

  • It implements RefUnit
  • It does not implement IsStatic
  • It has a different TypeId from &'static ().

This seems very strange.

#![feature(type_info)]

use std::any::TypeId;
use std::mem::type_info::{Type, TypeKind};

trait RefUnit {}
impl<'a> RefUnit for &'a () {}

trait IsStatic {}
impl<T: 'static> IsStatic for T {}

fn main() {
    let out = const {
        let x = Type::of::<for<'a> fn(&'a ()) -> &'a ()>();
        let TypeKind::FnPtr(fn_ptr) = x.kind else {
            panic!()
        };
        let out: TypeId = fn_ptr.output;
        // out is the TypeId of &'????? ()
        assert!(out.trait_info_of::<dyn RefUnit>().is_some());
        assert!(out.trait_info_of::<dyn IsStatic>().is_none());
        assert!(TypeId::of::<&'static ()>().trait_info_of::<dyn IsStatic>().is_some());
        out
    };
    assert!(out != TypeId::of::<&'static ()>());
}
Meta

Reproducible on the playground with version 1.95.0-nightly (2026-02-24 859951e3c7c9d0322c39)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided reproducer on Rust nightly 1.95.0 and inspect the Type::of and TypeKind::FnPtr behavior for for<'a> fn(&'a ()) -> &'a (). Determine the intended lifetime and TypeId semantics, then confirm that the resulting TypeId and trait information match that behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.