erg-lang / erg

A statically typed language compatible with Python
http://erg-lang.org
Apache License 2.0
2.68k stars 55 forks source link

Warn isolated type variables #446

Open mtshiba opened 1 year ago

mtshiba commented 1 year ago

Describe the code issue?

Recently, I implemented warn_isolated_vars.

impl TyVarCache {
    /// Warn when a type does not need to be a type variable, such as `|T| T -> Int` (it should be `Obj -> Int`).
    ///
    /// TODO: This warning is currently disabled because it raises a false warning in cases like `|T|(x: T) -> (y: T) -> (x, y)`.
    pub fn warn_isolated_vars(&self, ctx: &Context) {
        for (name, vi) in self.var_infos.iter() {
            let refs = &ctx.index().get_refs(&vi.def_loc).unwrap().referrers;
            if refs.len() == 1 {
                let warn = TyCheckError::unnecessary_tyvar_warning(
                    ctx.cfg.input.clone(),
                    line!() as usize,
                    vi.def_loc.loc,
                    name.inspect(),
                    ctx.caused_by(),
                );
                ctx.shared().warns.push(warn);
            }
        }
    }
}

As noted in the TODO, this is not currently enabled as it may cause a false warning. I can rewrite it correctly, but for now I have other tasks to do, so I will leave it as a code issue.

Additional context

No response

Erg version

0.6.18.nightly

Python version

None

OS

None