to type or not to type: quantifying detectable bugs in

68
To Type or Not to Type: Quantifying Detectable Bugs in JavaScript Zheng Gao , Christian Bird , Earl Barr University College London, Microsoft Research

Upload: others

Post on 21-Jan-2022

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: To Type or Not to Type: Quantifying Detectable Bugs in

To Type or Not to Type: Quantifying Detectable Bugs

in JavaScript

Zheng Gao✢, Christian Bird✵, Earl Barr✢

✢University College London, ✵Microsoft Research

Page 2: To Type or Not to Type: Quantifying Detectable Bugs in

Static Typing Dynamic Typingvs.

Page 3: To Type or Not to Type: Quantifying Detectable Bugs in

Static Typing

3

,91

9 3 3

1 3

3 3

1 1

5 3

5 31 3

Dynamic Typingvs.

Page 4: To Type or Not to Type: Quantifying Detectable Bugs in

Static Typing

1 3

Dynamic Typingvs.

Page 5: To Type or Not to Type: Quantifying Detectable Bugs in

Engine of the Web

is dynamically typed;

has a large set of long-running projects.{

Page 6: To Type or Not to Type: Quantifying Detectable Bugs in

Engine of the Web

is dynamically typed;

has a large set of long-running projects. 5 5 3 9

{

Page 7: To Type or Not to Type: Quantifying Detectable Bugs in

Static Typing for JavaScript

Page 8: To Type or Not to Type: Quantifying Detectable Bugs in

Static Typing for JavaScript

Page 9: To Type or Not to Type: Quantifying Detectable Bugs in

Had Static Typing been Used …

Page 10: To Type or Not to Type: Quantifying Detectable Bugs in

Had Static Typing been Used …

!

!

!

Page 11: To Type or Not to Type: Quantifying Detectable Bugs in

Had Static Typing been Used …

!

!

!

Page 12: To Type or Not to Type: Quantifying Detectable Bugs in

Had Static Typing been Used …

!

!

Page 13: To Type or Not to Type: Quantifying Detectable Bugs in

Central Finding

Page 14: To Type or Not to Type: Quantifying Detectable Bugs in

Central Finding

15%

Page 15: To Type or Not to Type: Quantifying Detectable Bugs in

Central Finding

15%

Page 16: To Type or Not to Type: Quantifying Detectable Bugs in

Bug Life Cycle

Page 17: To Type or Not to Type: Quantifying Detectable Bugs in

Bug Life Cycle

edit-timebugs

Page 18: To Type or Not to Type: Quantifying Detectable Bugs in

Bug Life Cycle

edit-timebugs

saved bugs

Page 19: To Type or Not to Type: Quantifying Detectable Bugs in

Bug Life Cycle

edit-timebugs

saved bugs

team bugs

Page 20: To Type or Not to Type: Quantifying Detectable Bugs in

Bug Life Cycle

edit-timebugs

saved bugs

team bugs

field bugs

Page 21: To Type or Not to Type: Quantifying Detectable Bugs in

Bug Life Cycle

team bugs

field bugs

Page 22: To Type or Not to Type: Quantifying Detectable Bugs in

public bugs

Bug Life Cycle

team bugs

field bugs

Page 23: To Type or Not to Type: Quantifying Detectable Bugs in

Type System Detectable

Definition (ts-detectable): Given a static type system ts, a bug is ts-detectable when

1. adding or changing type annotations causes the program containing the bug to fail to type check on a line a fix changes.

Page 24: To Type or Not to Type: Quantifying Detectable Bugs in

Problem

var a = b + 1;

var a:boolean = b + 1;

When the type of b is nullable number, annotating

to

“trivially” triggers a type error.

Page 25: To Type or Not to Type: Quantifying Detectable Bugs in

Consistency

Definition (Consistency): The added or changed type annotations are consistent with a fixed version of the program containing the bug f, if they carried to f type check, and the type of every annotated term is a supertype of that term’s type when an oracle precisely annotates it in f.

Page 26: To Type or Not to Type: Quantifying Detectable Bugs in

Type System Detectable

Definition (ts-detectable): Given a static type system ts, a bug is ts-detectable when

1. adding or changing type annotations causes the program containing the bug to fail to type check on a line a fix changes;

2. the new annotations are consistent with a fixed version of the program containing the bug.

Page 27: To Type or Not to Type: Quantifying Detectable Bugs in

Example of Detection

TypeScript throws the following error:

Error-free in JavaScript, and unexpectedly displays

an string, 30

Page 28: To Type or Not to Type: Quantifying Detectable Bugs in

Research Question

What percentage of public bugs are detectable under Flow or TypeScript?

Page 29: To Type or Not to Type: Quantifying Detectable Bugs in

Experiment Overview

patch

Page 30: To Type or Not to Type: Quantifying Detectable Bugs in

patch

Corpus Collection

Page 31: To Type or Not to Type: Quantifying Detectable Bugs in

patch

Corpus Collection

‣ What is the sample size?

‣ How to identify public bugs?

Page 32: To Type or Not to Type: Quantifying Detectable Bugs in

Corpus Collection

‣ What is the sample size?

‣ How to identify public bugs?

Page 33: To Type or Not to Type: Quantifying Detectable Bugs in

Sample Size Calculation

3,910,969closed bug

reports384 bugs

s: sample size X2: a constant for the confidence level of 95% N: population size, 3910969 P: population proportion, 0.5 d: degree of accuracy, 0.05

Page 34: To Type or Not to Type: Quantifying Detectable Bugs in

Corpus Collection

‣ What is the sample size?

‣ How to identify public bugs?

Page 35: To Type or Not to Type: Quantifying Detectable Bugs in

Bug Identification

Bug Identification

Page 36: To Type or Not to Type: Quantifying Detectable Bugs in

Bug Identification

Bug Identification

Fix Identification

Page 37: To Type or Not to Type: Quantifying Detectable Bugs in

Bug Identification

Parents arebuggy

Bug Identification

Fix Identification

Page 38: To Type or Not to Type: Quantifying Detectable Bugs in

Fix Identification

Page 39: To Type or Not to Type: Quantifying Detectable Bugs in

Fix Identification

These candidates may include commits that add

features or refactor.

Page 40: To Type or Not to Type: Quantifying Detectable Bugs in

Subjectsgeneral bugs

ts-detectable bugs

public bugs

Page 41: To Type or Not to Type: Quantifying Detectable Bugs in

Subjectsgeneral bugs

ts-detectable bugs

public bugs

Page 42: To Type or Not to Type: Quantifying Detectable Bugs in

Subjectsgeneral bugs

ts-detectable bugs

public bugs

fixed public bugs

Page 43: To Type or Not to Type: Quantifying Detectable Bugs in

Subjectsgeneral bugs

ts-detectable bugs

public bugs

fixed public bugs

Page 44: To Type or Not to Type: Quantifying Detectable Bugs in

Size Statistics of the Corpus

The sizes are in lines of code.

Page 45: To Type or Not to Type: Quantifying Detectable Bugs in

Methodology

Researcher

Project Version History

Page 46: To Type or Not to Type: Quantifying Detectable Bugs in

Methodology

buggyversion

fixedversion

Researcher

Project Version History

Page 47: To Type or Not to Type: Quantifying Detectable Bugs in

Methodology

buggyversion

fixedversion

travel back in timeProject Version History

Page 48: To Type or Not to Type: Quantifying Detectable Bugs in

Methodology

Check out pi-1Project Version History

Page 49: To Type or Not to Type: Quantifying Detectable Bugs in

Methodology

Gradually annotate pi-1Project Version History

Page 50: To Type or Not to Type: Quantifying Detectable Bugs in

Methodology

a is the annotation function

Project Version HistoryType check a(pi-1)

Page 51: To Type or Not to Type: Quantifying Detectable Bugs in

patch

Annotation

Page 52: To Type or Not to Type: Quantifying Detectable Bugs in

patch

Annotation

justify undetectable

N

Y

Y

type checkpossibly

detectable?

explain annotations

gradually add

annotationdetected?

N

Page 53: To Type or Not to Type: Quantifying Detectable Bugs in

patch

Annotation

justify undetectable

N

Y

Y

type checkpossibly

detectable?

explain annotations

gradually add

annotation

We do not fully annotate the program; we rely on gradual typing to locally, minimally annotate the

patched region.

detected?

N

Page 54: To Type or Not to Type: Quantifying Detectable Bugs in

Annotation Sources

bug fixes bug reports project documentation

Page 55: To Type or Not to Type: Quantifying Detectable Bugs in

Expert Source

Page 56: To Type or Not to Type: Quantifying Detectable Bugs in

Problem

var t = {x:0, z:1}; t.x = t.y; // the error is y does not exist on t t.x = t.z;

Page 57: To Type or Not to Type: Quantifying Detectable Bugs in

Problem

var t = {x:0, z:1}; t.x = t.y; // the error is y does not exist on t t.x = t.z;

What is the type of variable t?

Page 58: To Type or Not to Type: Quantifying Detectable Bugs in

Problem

var t = {x:0, z:1}; t.x = t.y; // the error is y does not exist on t t.x = t.z;

What is the type of variable t? Seems to be

{x: number, z: number}?

Page 59: To Type or Not to Type: Quantifying Detectable Bugs in

Problem

var t = {x:0, z:1}; t.x = t.y; // the error is y does not exist on t t.x = t.z;

What is the type of variable t? Seems to be

{x: number, z: number}?

Not necessarily!

Page 60: To Type or Not to Type: Quantifying Detectable Bugs in

Problem

var t = {x:0, z:1}; t.x = t.y; // the error is y does not exist on t t.x = t.z;

What is the type of variable t?

… … … t.x = “a”;

Seems to be {x: number, z: number}?

Not necessarily!

Page 61: To Type or Not to Type: Quantifying Detectable Bugs in

Problem

var t = {x:0, z:1}; t.x = t.y; // the error is y does not exist on t t.x = t.z;

What is the type of variable t?

… … … t.x = “a”;

Seems to be {x: number, z: number}?

Not necessarily!

Now becomes {x: number | string, z: number}.

Page 62: To Type or Not to Type: Quantifying Detectable Bugs in

Type Shims

A set of type bindings for the free identifiers that

1). is consistent with but

2). may not exist in

a fixed version of the program containing the bug.

Page 63: To Type or Not to Type: Quantifying Detectable Bugs in

Shim Examplevar t = {x:0, z:1}; t.x = t.y; // y does not exist on t t.x = t.z;

interface T { x:any; z:any; } var t:T = {x:0, z:1}; t.x = t.y; t.x = t.z;

This shim is consistent, as T

must be the supertype.

annotate

Page 64: To Type or Not to Type: Quantifying Detectable Bugs in

Annotation Quality

84% of the annotated fixed versions type check.

we add the same annotations to pi

Project Version HistoryType check a(pi)

Page 65: To Type or Not to Type: Quantifying Detectable Bugs in

Results

Both Flow and TypeScript detect 15% of the collected bugs; the confidence range is [11.5%,18.5%], at a 95% confidence level.

Page 66: To Type or Not to Type: Quantifying Detectable Bugs in

“That’s shocking. If you could make a change to the way we do development that would reduce the number of bugs being checked in by 10% or more overnight, that’s a no-brainer. Unless it doubles development time or something, we’d do it.”

- An engineering manager at Microsoft

Implications

Page 67: To Type or Not to Type: Quantifying Detectable Bugs in

Experimental Artefacts

http://ttendency.cs.ucl.ac.uk/projects/type_study/index.html

Page 68: To Type or Not to Type: Quantifying Detectable Bugs in

http://ttendency.cs.ucl.ac.uk/projects/type_study/index.html