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

Code that is optimized out is considered coverable but never covered #46012

Closed
Hixie opened this issue May 13, 2021 · 5 comments
Closed

Code that is optimized out is considered coverable but never covered #46012

Hixie opened this issue May 13, 2021 · 5 comments
Labels
area-vm Use area-vm for VM related issues, including code coverage, FFI, and the AOT and JIT backends.

Comments

@Hixie
Copy link
Contributor

Hixie commented May 13, 2021

lib/foo.dart:

class Test {
  Test(
  ) : assert(true),
      _a = 1,
      assert(true) {
    _a = 2;
  }

  int _a;
}

pubspec.yaml:

name: foo
dependencies:
  test: any
environment:
  sdk: '>2.12.0'

test/test_test.dart:

import 'package:test/test.dart';
import 'package:foo/foo.dart';

void main() {
  test('', () { Test(); });
}

Collect coverage however you wish, I used this:

$ flutter test --coverage
$ genhtml coverage/lcov.info

Expected result: 100% coverage.
Actual result: 50% coverage. Specifically:

class Test {
  Test( // COVERED
  ) : assert(true),  // NOT COVERED
      _a = 1,
      assert(true) { // NOT COVERED
    _a = 2; // COVERED
  }

  int _a;
}

In other words, asserts in constructors count against coverage. This seems suboptimal and is dragging down our coverage numbers over here in Flutter land because some of our libraries with 100% coverage actually score less than that and the only lines that aren't covered are the asserts.

@Hixie
Copy link
Contributor Author

Hixie commented May 14, 2021

I wonder if what's going on here is that asserts that are always true are marked as not covered, but are still marked as runnable code. It makes sense that they not count towards coverage (they're redundant no-op code), but in that case they should be marked as not coverable as well.

FWIW, this is actually making it quite hard for me to improve the coverage for Flutter because so many of our "uncovered" lines are things like this, where there's an assert that's optimized out and gets marked as untested.

@lrhn lrhn added the area-vm Use area-vm for VM related issues, including code coverage, FFI, and the AOT and JIT backends. label May 14, 2021
@lrhn
Copy link
Member

lrhn commented May 14, 2021

It would make sense if asserts didn't count towards coverage when assertions are disabled.

It's slightly tricky for const constructor assertions since they are always executed during const evaluation. I don't know how const evaluation counts towards coverage in general.

@Hixie
Copy link
Contributor Author

Hixie commented May 21, 2021

As far as I can tell what's actually going on is that the asserts (and other code, I don't think it's strictly only asserts) that the compiler can prove are always true (or are always dead code) are being optimised out, but still count towards code that could be covered, and thus end up as "uncovered" code in the coverage results.

Code that's optimized out should just count as uncoverable, like blank lines or comments.

cc @liamappelbe

@Hixie Hixie changed the title Asserts in constructors count against coverage Code that is optimized out is considered coverable but never covered Aug 26, 2021
@liamappelbe
Copy link
Contributor

I think this bug is the same underlying issue as #47017. Asserts just weren't having their coverage tracked properly. Should be fixed now.

@Hixie
Copy link
Contributor Author

Hixie commented Nov 11, 2021

woot! thanks @liamappelbe!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, FFI, and the AOT and JIT backends.
Projects
None yet
Development

No branches or pull requests

3 participants