google-research / google-research/dex-lang

Add variable shadowing warning

Open
#338 0 comments 1 reaction 0 assignees View on GitHub
language / concrete syntax
Dominant language
Haskell
Stars
1.7k
Forks
117
PR merge metrics
No merged PRs in 30d

Description

Some test cases:

```
-- Value shadows value
def addThree (x:Int) : Int =
x = x + 1 -- redefinition of x, warn here
x = x + 2 -- redefinition of x, warn here
x

:p addThree 3
> 6

-- Value shadows type
def shadow (array: x=>Int) : Int =
y:x = 0@x -- use of x as type
x = size x -- redefinition of x as value, warn here
x

:p shadow (iota $ Fin 10)
> 10
```

Motivating use case from @srush: https://www.twitch.tv/videos/833913502?t=01h00m17s

---

Reference Haskell program and compiler diagnostics:

```
module Shadow where

addThree :: Int -> Int
addThree x =
let x = x + 1 in
let x = x + 2 in
x
```
```console
$ ghc -c -Wall Shadow.hs

Shadow.hs:4:10: warning: [-Wunused-matches]
Defined but not used: ‘x’
|
4 | addThree x =
| ^

Shadow.hs:5:7: warning: [-Wname-shadowing]
This binding for ‘x’ shadows the existing binding
bound at Shadow.hs:4:10
|
5 | let x = x + 1 in
| ^

Shadow.hs:5:7: warning: [-Wunused-local-binds]
Defined but not used: ‘x’
|
5 | let x = x + 1 in
| ^

Shadow.hs:5:11: warning: [-Wtype-defaults]
• Defaulting the following constraint to type ‘Integer’
Num a0 arising from a use of ‘+’
• In the expression: x + 1
In an equation for ‘x’: x = x + 1
In the expression: let x = x + 1 in let x = x + 2 in x
|
5 | let x = x + 1 in
| ^^^^^

Shadow.hs:6:7: warning: [-Wname-shadowing]
This binding for ‘x’ shadows the existing binding
bound at Shadow.hs:5:7
|
6 | let x = x + 2 in
| ^
```

Contributor guide

Open the contributing guide

Research direction

No file or test entry point is named. Start by locating the compiler's handling of value and type bindings, then compare its diagnostics with the addThree and shadow examples; done means those redefinitions produce warnings at the indicated locations.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.