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

RA fails to infere types #18276

Closed
xenoliss opened this issue Oct 10, 2024 · 4 comments
Closed

RA fails to infere types #18276

xenoliss opened this issue Oct 10, 2024 · 4 comments
Labels
A-ty type system / type inference / traits / method resolution C-bug Category: bug

Comments

@xenoliss
Copy link

rust-analyzer version: (eg. output of "rust-analyzer: Show RA Version" command, accessible in VSCode via Ctrl/⌘+Shift+P)
0.4.2141-standalone (0fb804a 2024-10-09) [/home/vscode/.vscode-server/extensions/rust-lang.rust-analyzer-0.4.2141-linux-arm64/server/rust-analyzer]

rustc version: (eg. output of rustc -V)
rustc 1.82.0-nightly (fbccf5053 2024-07-27)

editor or extension: (eg. VSCode, Vim, Emacs, etc. For VSCode users, specify your extension version; for users of other editors, provide the distribution if applicable)
VSCode

relevant settings: (eg. client settings, or environment variables like CARGO, RUSTC, RUSTUP_HOME or CARGO_HOME)

repository link (if public, optional): (eg. rust-analyzer)
https://github.com/xenoliss/ra-issue-type-inference

code snippet to reproduce:
See https://github.com/xenoliss/ra-issue-type-inference/blob/main/src/main.rs

@xenoliss xenoliss added the C-bug Category: bug label Oct 10, 2024
@ShoyuVanilla ShoyuVanilla added the A-ty type system / type inference / traits / method resolution label Oct 10, 2024
@detrumi
Copy link
Member

detrumi commented Dec 30, 2024

Slightly simplified to:

[dependencies]
ark-bn254 = "0.4.0"
ark-ec = "0.4.2"
ark-ff = "0.4.2"
use std::marker::PhantomData;

use ark_bn254::{Fq, Fq2, Fq2Config, Fq6Config, Fq12Config, g1, g2};
use ark_ec::{
    bn::{Bn, BnConfig, TwistType},
    pairing::Pairing,
};
use ark_ff::{MontFp, PrimeField};

struct Config;
impl BnConfig for Config {
    const X: &'static [u64] = &[4965661367192848881];
    const X_IS_NEGATIVE: bool = false;
    const ATE_LOOP_COUNT: &'static [i8] = &[
        0, 0, 0, 1, 0, 1, 0, -1, 0, 0, 1, -1, 0, 0, 1, 0, 0, 1, 1, 0, -1, 0, 0, 1, 0, -1, 0, 0, 0,
        0, 1, 1, 1, 0, 0, -1, 0, 0, 1, 0, 0, 0, 0, 0, -1, 0, 0, 1, 1, 0, 0, -1, 0, 0, 0, 1, 1, 0,
        -1, 0, 0, 1, 0, 1, 1,
    ];

    const TWIST_MUL_BY_Q_X: Fq2 = Fq2::new(
        MontFp!("21575463638280843010398324269430826099269044274347216827212613867836435027261"),
        MontFp!("10307601595873709700152284273816112264069230130616436755625194854815875713954"),
    );
    const TWIST_MUL_BY_Q_Y: Fq2 = Fq2::new(
        MontFp!("2821565182194536844548159561693502659359617185244120367078079554186484126554"),
        MontFp!("3505843767911556378687030309984248845540243509899259641013678093033130930403"),
    );
    const TWIST_TYPE: TwistType = TwistType::D;
    type Fp = Fq;
    type Fp2Config = Fq2Config;
    type Fp6Config = Fq6Config;
    type Fp12Config = Fq12Config;
    type G1Config = g1::Config;
    type G2Config = g2::Config;
}

type Bn254 = Bn<Config>;

pub struct KZG10<P: PrimeField> {
    _poly: PhantomData<P>,
}

impl<P> KZG10<P>
where
    P: PrimeField,
{
    pub fn setup() {}
}

fn main() {
    let _params = KZG10::<<Bn254 as Pairing>::ScalarField>::setup();
}

The MontFp!() macros throw a proc-macro panicked: could not parse (also with MontFp("1")).
Not sure whether that's a problem with rust-analyzer, the macro, or both.

@detrumi
Copy link
Member

detrumi commented Jan 3, 2025

After some more digging, it turned out to be a different problem.
The culprit was the MontConfig proc macro derive for FrConfig.

This expands to the following:

pub struct FrConfig;
fn frconfig___() {
    use ark_ff::{
        fields::Fp, BigInt, BigInteger, biginteger::arithmetic as fa, fields::*,
    };
    type B = BigInt<4usize>;
    type F = Fp<MontBackend<FrConfig, 4usize>, 4usize>;
    #[automatically_derived]
    impl MontConfig<4usize> for FrConfig {
      ...
    }
}

rust-analyzer doesn't seem to pick up the impl in this function, while rustc does (though it warns about a non-local impl).
Changing frconfig___ from function to a module fixes the problem.

Full repro
// [dependencies]
// ark-bn254 = "0.4.0"
// ark-ff = "0.4.2"

use std::marker::PhantomData;

use ark_ff::MontConfig;

pub struct MontBackend<T: MontConfig<N>, const N: usize>(PhantomData<T>);

trait PrimeField {}

impl<T: MontConfig<N>, const N: usize> PrimeField for MontBackend<T, N> {}

struct KZG10<P: PrimeField> {
    _poly: PhantomData<P>,
}

impl<P> KZG10<P>
where
    P: PrimeField,
{
    pub fn setup() {}
}

pub struct FrConfig;
fn frconfig__() {
    use ark_ff::{BigInt, fields::Fp, fields::*};
    type F = Fp<MontBackend<FrConfig, 4usize>, 4usize>;
    impl MontConfig<4usize> for FrConfig {
        const MODULUS: BigInt<4usize> = BigInt::one();
        const GENERATOR: F = Fp::new(BigInt::one());
        const TWO_ADIC_ROOT_OF_UNITY: F =
            { ::ark_ff::Fp::from_sign_and_limbs(true, &[0, 0, 0, 0]) };
    }
}

fn main() {
    let _params = KZG10::<MontBackend<FrConfig, 4>>::setup();
}

Minimal example of the problem:

struct S1 {}
struct S2 {}
trait T {
    fn foo() {}
}

fn fails() {
    impl T for S1 {
        fn foo() {}
    }
}

mod works {
    impl crate::T for crate::S2 {
        fn foo() {}
    }
}

fn main() {
    let _foo1 = S1::foo(); // {unknown}
    let _foo2 = S2::foo(); // ()
}

@lnicola
Copy link
Member

lnicola commented Jan 3, 2025

We don't (want to) support function-local trait impls.

@lnicola
Copy link
Member

lnicola commented Jan 4, 2025

@lnicola lnicola closed this as not planned Won't fix, can't repro, duplicate, stale Jan 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ty type system / type inference / traits / method resolution C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

4 participants