Summary: Litho uses an idiom as follows. `Component.Builder` has an abstract method `T getThis()` which is supposed to be implemented as `{return this;}`. I believe the reason it's there is to have covariant return types in subclasses, though this doesn't really matter. We don't do dynamic dispatch, so instead of summarising `getThis` as having a return ownership of `OwnedIf({ 0 })` we just return `Unowned`. This is generating many false positives, which are really hard to debug. Here I'm special-casing any abstract method whose return type is equal to that of it's only argument, to return conditional ownership. Reviewed By: da319 Differential Revision: D8947992 fbshipit-source-id: 33c7e952dmaster
parent
ee7f07a1a9
commit
6b156f71fe
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright (c) 2016-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package codetoanalyze.java.checkers;
|
||||
|
||||
import com.facebook.infer.annotation.ReturnsOwnership;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
// no races should be reported here
|
||||
// abstract getThis should get a default summary returning conditional ownership
|
||||
|
||||
@ThreadSafe
|
||||
abstract class Component {
|
||||
abstract static class Builder<T extends Builder<T>> {
|
||||
abstract T getThis();
|
||||
|
||||
private int i;
|
||||
|
||||
public T set(int i) {
|
||||
this.i = i;
|
||||
return getThis();
|
||||
}
|
||||
|
||||
public T background() {
|
||||
return getThis();
|
||||
}
|
||||
|
||||
@ReturnsOwnership
|
||||
abstract Component build();
|
||||
}
|
||||
}
|
||||
|
||||
@ThreadSafe
|
||||
class Column extends Component {
|
||||
static Component onCreateLayoutOk() {
|
||||
Component.Builder<?> builder = ColumnBuilder.create().background();
|
||||
return builder.set(0).build();
|
||||
}
|
||||
|
||||
static class ColumnBuilder extends Component.Builder<ColumnBuilder> {
|
||||
static ColumnBuilder create() {
|
||||
return new ColumnBuilder();
|
||||
}
|
||||
|
||||
@Override
|
||||
ColumnBuilder getThis() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
Column build() {
|
||||
return new Column();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue