Skip to content

Commit

Permalink
Revert "Revert "Remove Mutator support from Prop.cc (sorbet#3180)" (s…
Browse files Browse the repository at this point in the history
…orbet#3196)" (sorbet#3198)

This reverts commit 0930372.
  • Loading branch information
jez authored Jun 15, 2020
1 parent 4fb3a6e commit c303238
Show file tree
Hide file tree
Showing 15 changed files with 1 addition and 993 deletions.
4 changes: 0 additions & 4 deletions core/tools/generate_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,8 @@ NameDef names[] = {
{"Chalk", "Chalk", true},
{"ODM", "ODM", true},
{"Document", "Document", true},
{"Mutator", "Mutator", true},
{"Private", "Private", true},
{"Types", "Types", true},
{"HashMutator", "HashMutator", true},
{"ArrayMutator", "ArrayMutator", true},
{"DocumentMutator", "DocumentMutator", true},
{"DocumentDecoratorHelper", "DocumentDecoratorHelper", true},
{"Chalk_ODM_Document", "::Chalk::ODM::Document"},

Expand Down
31 changes: 0 additions & 31 deletions rbi/gems/chalk.rbi
Original file line number Diff line number Diff line change
@@ -1,37 +1,6 @@
# The minimal portion of our Chalk::ODM support
# typed: __STDLIB_INTERNAL

class Chalk::ODM::Mutator
end

class Chalk::ODM::Mutator::Private::HashMutator
extend T::Generic

K = type_member
V = type_member

sig {params(key: K, value: V).void}
def []=(key, value)
end
end

class Chalk::ODM::Mutator::Private::ArrayMutator
extend T::Generic

Elem = type_member

sig {params(value: Elem).void}
def <<(value)
end

sig {params(key: Integer, value: Elem).void}
def []=(key, value)
end
end

class Chalk::ODM::Mutator::Private::DocumentMutator
end

class Chalk::ODM::Document
include T::Props
include T::Props::Serializable
Expand Down
15 changes: 0 additions & 15 deletions rewriter/MixinEncryptedProp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,6 @@ vector<ast::TreePtr> MixinEncryptedProp::run(core::MutableContext ctx, ast::Send
stats.emplace_back(ASTUtil::mkSet(ctx, loc, setEncName, nameLoc, ast::MK::RaiseUnimplemented(loc)));
}

// Compute the Mutator
{
// Compute a setter
ast::ClassDef::RHS_store rhs;
rhs.emplace_back(
ast::MK::Sig(loc, ast::MK::Hash1(nameLoc, ast::MK::Symbol(loc, core::Names::arg0()), mkNilableString(loc)),
mkNilableString(loc)));
rhs.emplace_back(ASTUtil::mkSet(ctx, loc, setName, nameLoc, ast::MK::RaiseUnimplemented(loc)));

rhs.emplace_back(ast::MK::Sig(
loc, ast::MK::Hash1(loc, ast::MK::Symbol(nameLoc, core::Names::arg0()), mkNilableEncryptedValue(ctx, loc)),
mkNilableEncryptedValue(ctx, loc)));
rhs.emplace_back(ASTUtil::mkSet(ctx, loc, setEncName, nameLoc, ast::MK::RaiseUnimplemented(loc)));
}

return stats;
}

Expand Down
6 changes: 0 additions & 6 deletions rewriter/MixinEncryptedProp.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ namespace sorbet::rewriter {
* def encrypted_foo; T.cast(nil, T.nilable(String)); end
* sig {params(arg0: String).returns(NilClass)}
* def encrypted_foo=(arg0); end
* class Mutator < Chalk::ODM::Mutator
* sig {params(arg0: String).returns(NilClass)}
* def foo=(arg0); end
* sig {params(arg0: String).returns(NilClass)}
* def encrypted_foo=(arg0); end
* end
*
* We try to implement a simple approximation of the functionality that
* M::Mixins::Encryptable.encrypted_prop has. This isn't full fidelity, but we're trying to
Expand Down
49 changes: 0 additions & 49 deletions rewriter/Prop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -423,55 +423,6 @@ vector<ast::TreePtr> processProp(core::MutableContext ctx, PropInfo &ret, PropCo
ast::MK::RaiseUnimplemented(loc)));
}

// Compute the Mutator
{
// Compute a setter
auto setType = ASTUtil::dupType(ret.type);
ast::ClassDef::RHS_store rhs;
rhs.emplace_back(ast::MK::Sig(
loc, ast::MK::Hash1(loc, ast::MK::Symbol(nameLoc, core::Names::arg0()), ASTUtil::dupType(setType)),
ASTUtil::dupType(setType)));
rhs.emplace_back(ASTUtil::mkSet(ctx, loc, setName, nameLoc, ast::MK::RaiseUnimplemented(loc)));

// Maybe make a getter
ast::TreePtr mutator;
if (ASTUtil::isProbablySymbol(ctx, ret.type, core::Symbols::Hash())) {
mutator = ASTUtil::mkMutator(ctx, loc, core::Names::Constants::HashMutator());
auto *send = ast::cast_tree_const<ast::Send>(ret.type);
if (send && send->fun == core::Names::squareBrackets() && send->args.size() == 2) {
mutator = ast::MK::Send2(loc, std::move(mutator), core::Names::squareBrackets(),
ASTUtil::dupType(send->args[0]), ASTUtil::dupType(send->args[1]));
} else {
mutator = ast::MK::Send2(loc, std::move(mutator), core::Names::squareBrackets(), ast::MK::Untyped(loc),
ast::MK::Untyped(loc));
}
} else if (ASTUtil::isProbablySymbol(ctx, ret.type, core::Symbols::Array())) {
mutator = ASTUtil::mkMutator(ctx, loc, core::Names::Constants::ArrayMutator());
auto *send = ast::cast_tree_const<ast::Send>(ret.type);
if (send && send->fun == core::Names::squareBrackets() && send->args.size() == 1) {
mutator = ast::MK::Send1(loc, std::move(mutator), core::Names::squareBrackets(),
ASTUtil::dupType(send->args[0]));
} else {
mutator = ast::MK::Send1(loc, std::move(mutator), core::Names::squareBrackets(), ast::MK::Untyped(loc));
}
} else if (ast::isa_tree<ast::UnresolvedConstantLit>(ret.type)) {
// In a perfect world we could know if there was a Mutator we could reference instead, like this:
// mutator = ast::MK::UnresolvedConstant(loc, ASTUtil::dupType(type.get()),
// core::Names::Constants::Mutator()); For now we're just going to leave these in method_missing.rbi
}

if (mutator.get()) {
rhs.emplace_back(ast::MK::Sig0(loc, ASTUtil::dupType(mutator)));
rhs.emplace_back(ASTUtil::mkGet(ctx, loc, name, ast::MK::RaiseUnimplemented(loc)));

ast::ClassDef::ANCESTORS_store ancestors;
auto name = core::Names::Constants::Mutator();
nodes.emplace_back(ast::MK::Class(loc, core::Loc(ctx.file, loc),
ast::MK::UnresolvedConstant(loc, ast::MK::EmptyTree(), name),
std::move(ancestors), std::move(rhs)));
}
}

return nodes;
}

Expand Down
6 changes: 0 additions & 6 deletions rewriter/Prop.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ namespace sorbet::rewriter {
* def foo; ...; end
* sig {params(arg0: Type).returns(Type)}
* def foo=(arg0); ...; end
* class Mutator < Chalk::ODM::Mutator
* sig {returns(Type)}
* def foo; end
* sig {params(arg0: Type).returns(Type)}
* def foo=(arg0); end
* end
*
* We try to implement a simple approximation of the functionality that Chalk::ODM::Document.prop has. Any deviation
* from the expected shape stops the desugaring.
Expand Down
40 changes: 0 additions & 40 deletions rewriter/Util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,6 @@ ast::TreePtr ASTUtil::mkNilable(core::LocOffsets loc, ast::TreePtr type) {
return ast::MK::Send1(loc, ast::MK::T(loc), core::Names::nilable(), move(type));
}

ast::TreePtr ASTUtil::mkMutator(core::MutableContext ctx, core::LocOffsets loc, core::NameRef className) {
auto chalk = ast::MK::UnresolvedConstant(loc, ast::MK::Constant(loc, core::Symbols::root()),
core::Names::Constants::Chalk());
auto odm = ast::MK::UnresolvedConstant(loc, move(chalk), core::Names::Constants::ODM());
auto mutator = ast::MK::UnresolvedConstant(loc, move(odm), core::Names::Constants::Mutator());
auto private_ = ast::MK::UnresolvedConstant(loc, move(mutator), core::Names::Constants::Private());
return ast::MK::UnresolvedConstant(loc, move(private_), className);
}

ast::TreePtr ASTUtil::thunkBody(core::MutableContext ctx, ast::TreePtr &node) {
auto *send = ast::cast_tree<ast::Send>(node);
if (send == nullptr) {
Expand All @@ -220,35 +211,4 @@ ast::TreePtr ASTUtil::thunkBody(core::MutableContext ctx, ast::TreePtr &node) {
return std::move(block->body);
}

bool ASTUtil::isProbablySymbol(core::MutableContext ctx, ast::TreePtr &type, core::SymbolRef sym) {
auto *cnst = ast::cast_tree<ast::UnresolvedConstantLit>(type);
if (cnst) {
if (cnst->cnst != sym.data(ctx)->name) {
return false;
}
if (ast::isa_tree<ast::EmptyTree>(cnst->scope)) {
return true;
}

auto scopeCnst = ast::cast_tree<ast::UnresolvedConstantLit>(cnst->scope);
if (scopeCnst && ast::isa_tree<ast::EmptyTree>(scopeCnst->scope) &&
scopeCnst->cnst == core::Symbols::T().data(ctx)->name) {
return true;
}

auto scopeCnstLit = ast::cast_tree<ast::ConstantLit>(cnst->scope);
if (scopeCnstLit && scopeCnstLit->symbol == core::Symbols::root()) {
return true;
}
return false;
}

auto *send = ast::cast_tree<ast::Send>(type);
if (send && send->fun == core::Names::squareBrackets() && isProbablySymbol(ctx, send->recv, sym)) {
return true;
}

return false;
}

} // namespace sorbet::rewriter
4 changes: 0 additions & 4 deletions rewriter/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@ class ASTUtil {

static ast::TreePtr mkNilable(core::LocOffsets loc, ast::TreePtr type);

static ast::TreePtr mkMutator(core::MutableContext ctx, core::LocOffsets loc, core::NameRef className);

static ast::TreePtr thunkBody(core::MutableContext ctx, ast::TreePtr &node);

static bool isProbablySymbol(core::MutableContext ctx, ast::TreePtr &type, core::SymbolRef sym);

ASTUtil() = delete;
};
} // namespace sorbet::rewriter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

class Foo::MyModel
include T::Props
prop :array_of_explicit, Array, array: String
prop :array_of_explicit, T::Array[String]
end
22 changes: 0 additions & 22 deletions test/testdata/rewriter/not_prop.rb.rewrite-tree.exp
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,6 @@ class <emptyTree><<C <root>>> < (::<todo sym>)

::Sorbet::Private::Static.keep_def(<self>, :"array_of_explicit=")

class <emptyTree>::<C Mutator><<C <todo sym>>> < ()
::Sorbet::Private::Static.sig(::T::Sig::WithoutRuntime) do ||
<self>.params({:"arg0" => <emptyTree>::<C Array>}).returns(<emptyTree>::<C Array>)
end

def array_of_explicit=<<C <todo sym>>>(arg0, &<blk>)
::T.unsafe(::Kernel).raise("Sorbet rewriter pass partially unimplemented")
end

::Sorbet::Private::Static.sig(::T::Sig::WithoutRuntime) do ||
<self>.returns(::<root>::<C Chalk>::<C ODM>::<C Mutator>::<C Private>::<C ArrayMutator>.[](::T.untyped()))
end

def array_of_explicit<<C <todo sym>>>(&<blk>)
::T.unsafe(::Kernel).raise("Sorbet rewriter pass partially unimplemented")
end

::Sorbet::Private::Static.keep_def(<self>, :"array_of_explicit=")

::Sorbet::Private::Static.keep_def(<self>, :"array_of_explicit")
end

<self>.prop(:"no_class_arg", {:"type" => <emptyTree>::<C Array>, :"immutable" => true, :"array" => <emptyTree>::<C String>})

<self>.prop(:"proc_type", {:"type" => <emptyTree>::<C T>.proc().params({:"x" => <emptyTree>::<C Integer>}).void()})
Expand Down
10 changes: 0 additions & 10 deletions test/testdata/rewriter/not_prop.rb.symbol-table.exp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,6 @@ class ::<root> < ::Object ()
method ::<Class:<root>>#<static-init> (<blk>) @ test/testdata/rewriter/not_prop.rb:3
argument <blk><block> @ Loc {file=test/testdata/rewriter/not_prop.rb start=??? end=???}
class ::ThingsWhichUsedToBePropSyntax < ::Object () @ test/testdata/rewriter/not_prop.rb:3
class ::ThingsWhichUsedToBePropSyntax::Mutator < ::Object () @ test/testdata/rewriter/not_prop.rb:7
method ::ThingsWhichUsedToBePropSyntax::Mutator#array_of_explicit (<blk>) -> Chalk::ODM::Mutator::Private::ArrayMutator[T.untyped] @ test/testdata/rewriter/not_prop.rb:7
argument <blk><block> -> T.untyped @ Loc {file=test/testdata/rewriter/not_prop.rb start=??? end=???}
method ::ThingsWhichUsedToBePropSyntax::Mutator#array_of_explicit= (array_of_explicit, <blk>) -> T::Array[T.untyped] @ test/testdata/rewriter/not_prop.rb:7
argument array_of_explicit<> -> T::Array[T.untyped] @ Loc {file=test/testdata/rewriter/not_prop.rb start=7:9 end=7:26}
argument <blk><block> -> T.untyped @ Loc {file=test/testdata/rewriter/not_prop.rb start=??? end=???}
class ::ThingsWhichUsedToBePropSyntax::<Class:Mutator>[<AttachedClass>] < ::<Class:Object> () @ test/testdata/rewriter/not_prop.rb:7
type-member(+) ::ThingsWhichUsedToBePropSyntax::<Class:Mutator>::<AttachedClass> -> T.attached_class (of ThingsWhichUsedToBePropSyntax::Mutator) @ test/testdata/rewriter/not_prop.rb:7
method ::ThingsWhichUsedToBePropSyntax::<Class:Mutator>#<static-init> (<blk>) @ test/testdata/rewriter/not_prop.rb:7
argument <blk><block> @ Loc {file=test/testdata/rewriter/not_prop.rb start=??? end=???}
method ::ThingsWhichUsedToBePropSyntax#array_of_explicit (<blk>) -> T::Array[T.untyped] @ test/testdata/rewriter/not_prop.rb:7
argument <blk><block> -> T.untyped @ Loc {file=test/testdata/rewriter/not_prop.rb start=??? end=???}
method ::ThingsWhichUsedToBePropSyntax#array_of_explicit= (array_of_explicit, <blk>) -> T::Array[T.untyped] @ test/testdata/rewriter/not_prop.rb:7
Expand Down
Loading

0 comments on commit c303238

Please sign in to comment.