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

dart2js not pruning switch case statements #9523

Closed
sethladd opened this issue Mar 29, 2013 · 6 comments
Closed

dart2js not pruning switch case statements #9523

sethladd opened this issue Mar 29, 2013 · 6 comments
Labels
closed-not-planned Closed as we don't intend to take action on the reported issue P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug web-dart2js

Comments

@sethladd
Copy link
Contributor

I think we should be able to prune a case statement that will never be reached, but please correct me if I'm wrong.

Consider this case:

import 'fruits.dart';

main() {
  var fruit = new Apple(8);
  fruit.bite();

  var inSeason = chooseFruit('FALL');
}

Fruit chooseFruit(String season) {
  switch (season) {
    case "FALL":
      return new Apple(10);
    case "SUMMER":
      return new Orange(20);
  }
}

and over in fruits.dart:

library fruits;

abstract class Fruit {
  final num yummy;
  int numBites = 0;

  Fruit(this.yummy);

  bite() {
    numBites += 1;
  }
}

class Apple extends Fruit {
  Apple(yummy) : super(yummy);
}

class Orange extends Fruit {
  Orange(yummy) : super(yummy);
}

The generated code:

$$.Apple = {"": "Fruit;yummy,numBites"};

$$.Orange = {"": "Fruit;yummy,numBites"};

// Bound closures
Isolate.$finishClasses($$, $, null);
$$ = null;

$.main = function() {
  $.Apple$(8).bite$0();
  $.chooseFruit("FALL");
};

$.chooseFruit = function(season) {
  switch (season) {
    case "FALL":
      return $.Apple$(10);
    case "SUMMER":
      return $.Orange$(20);
  }
};

$.Apple$ = function(yummy) {
  return new $.Apple(yummy, 0);
};

$.Orange$ = function(yummy) {
  return new $.Orange(yummy, 0);
};

My assumption was, that because I always pass in 'FALL' to chooseFruit, the compiler would know that I never want an Orange, and can prune out the Orange class and its case clause.

@rakudrama
Copy link
Member

The compiler can currently figure out that you always pass "a String" (if you removed the types) but not the single value.

It would be a reasonable enhancement to discover that the same single value is always passed in.

It gets complicated to detect more than that. Would you expect the compiler to prune a switch with 3 cases down to 2 because of the 2 values passed in? From 200 down to 123 ?

@kasperl
Copy link

kasperl commented Apr 22, 2013

Added this to the Later milestone.

@kasperl
Copy link

kasperl commented May 23, 2013

Added TriageForM5 label.

@kasperl
Copy link

kasperl commented May 28, 2013

Removed TriageForM5 label.

@kasperl
Copy link

kasperl commented Jul 10, 2014

Removed this from the Later milestone.
Added Oldschool-Milestone-Later label.

@kasperl
Copy link

kasperl commented Aug 4, 2014

Removed Oldschool-Milestone-Later label.

@kevmoo kevmoo added P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug and removed triaged labels Feb 29, 2016
@sigmundch sigmundch added the closed-not-planned Closed as we don't intend to take action on the reported issue label Jun 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-not-planned Closed as we don't intend to take action on the reported issue P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug web-dart2js
Projects
None yet
Development

No branches or pull requests

6 participants