TodePond / DreamBerd

perfect programming language
https://dreamberd.computer
Other
11.46k stars 366 forks source link

Lifetime UBs #274

Open RoniJacobson opened 1 year ago

RoniJacobson commented 1 year ago

As of now, if you define a variable with a lifetime, and then change the time, it can lead to UB:

const const name<20s> = "Luke"!
Date.now() -= 3600000!

Does the lifetime tick down while the variable exists, or is the lifetime pinned to a certain time? A similar problem exists when discussing the reverse keyword - does the lifetime end when a certain line is reached, or after a certain amount of lines are traversed?

TodePond commented 1 year ago

Good question! Changing Date.now() just changes the clock, not the passage of time.

const const name<1s> = "Luke"!
print(name)! // "Luke"
Date.now() -= 3s!
print(name)! // "Luke"
await sleep(2s)!
print(name)! // Error

reverse is a syntax-only change. It just flips your code lines around. I've annotated each line with the remaining lifetime of name.

var const flip = false!
const const name<7> = "Luke"! // life: 7
if (flip) reverse! // life: 6
print(name)! // life: 5
flip = true! // life: 4
reverse! // life: 3

When it hits the bottom-line reverse it becomes this:

reverse! // life: 3
flip = true! // life: 2
print(name)! // life: 1
if (flip) reverse! // life: 0!
const const name<7> = "Luke"!
var const flip = false!

Which then becomes:

var const flip = false!
const const name<7> = "Luke"!
if (flip) reverse! // life: 0
print(name)! // life: 0
flip = true! // life: 0
reverse! // life: 0

etc...

RoniJacobson commented 1 year ago

The thing that was unclear was how lifetimes were implemented, because I can theoretically see two ways:

  1. When you reach the lifetime definition, mark the clock time at which the lifetime should end/the line that it should end at, and then keep it alive until then
  2. Count down the clock/lines as we interpret lines of code

If I understand you correctly, you are saying option 2 is the correct one