Skip to content

Comments

Comments/C++headers/Changelog entry for lambda comparison#7809

Merged
wilzbach merged 1 commit intodlang:masterfrom
RazvanN7:Lambda_part2
Feb 5, 2018
Merged

Comments/C++headers/Changelog entry for lambda comparison#7809
wilzbach merged 1 commit intodlang:masterfrom
RazvanN7:Lambda_part2

Conversation

@RazvanN7
Copy link
Contributor

@dlang-bot
Copy link
Contributor

Thanks for your pull request, @RazvanN7! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.

Some tips to help speed things up:

  • smaller, focused PRs are easier to review than big ones

  • try not to mix up refactoring or style changes with bug fixes or feature enhancements

  • provide helpful commit messages explaining the rationale behind each change

Bear in mind that large or tricky changes may require multiple rounds of review and revision.

Please see CONTRIBUTING.md for more information.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

{
v.getConstInitializer.accept(this);
}
// Otherwise, the function is uncomparable

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This flow is a little confusing.
please let the buf.reset be a fall-through case.

Copy link
Contributor Author

@RazvanN7 RazvanN7 Jan 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is a fall-through case, I just need it to be reset inside the first if-block if the var declaration isn't a manifest constant. I don't know now to do it differently without adding a label, which in my opinion would be worse.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Easy, use return. Then you only need to call buf.reset at the end.

Copy link

@stefan-koch-sociomantic stefan-koch-sociomantic Jan 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it could also be {auto v = s.isVarDeclaration if (v && (v.storage_class & STC.manifest))}

In general if (a) {if (b) {c}} maps exactly to if (a && b) {c}

else
buf.reset();
}
// Or it may be an enum

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant comment


-> the lambda function arguments must not have a template instantiation as an explicit argument type. Any other argument types (basic, user-defined, template) are supported.

-> the lambda function body must contain a single expression (no return statement) which contains only numeric values, manifest constants, enum values and arguments. If the expression contains local variables, function calls or return statements, the function is considered uncomparable.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more rational wanted.
why are the limitations the way they are ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the point I got to. Later on, the limitations will be lifted (where possible).

Copy link

@stefan-koch-sociomantic stefan-koch-sociomantic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's better but could be improved.

meta concern,
why would you serialize to a string and then compare that ?
it's expensive.

@RazvanN7
Copy link
Contributor Author

why would you serialize to a string and then compare that ?

To do parameter renaming and hopefully, later on, do some other computation (e.g. to be able to distinguish between a+b and b+a ). What is the alternative you are thinking?

* found when visiting the AST nodes to higher levels of the
* AST.
*/
enum ExpType
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this can be private.

* The serialization of this.
* != null only if tok == TOKreserved.
*/
const(char)* serialization;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could have a better description of what it's used for.

If the expression contains local variables, function calls or return statements,
the function is considered uncomparable.

These limitation will be lifted in the next release version.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These limitation will be lifted in the next release version.

I would recommend avoiding being this specific. There are not guarantees that those changes will make it into the next release. I would say something like: "These limitations might be lifted in the future".

@jacob-carlborg
Copy link
Contributor

  • What happens if two lambdas are not comparable? Will compiler give an error or will you get back false in the comparison?

  • Does SerializeVisitor need to be exposed in a C++ header?

correctly compare two lambdas, the following conditions must be
satisfied:

-> the lambda function arguments must not have a template instantiation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Until we have MarkDown support, the following is recommended:

$(UL
    $(LI ..)
)

src/dmd/func.d Outdated

/**
* The serialization of this.
* != null only if tok == TOKreserved.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TOK.reserved

/***
* Compiler implementation of the
* $(LINK2 http://www.dlang.org, D programming language).
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Describe the module here.

@@ -1,3 +1,15 @@
/***
* Compiler implementation of the
* $(LINK2 http://www.dlang.org, D programming language).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that this is the default header, but I don't like it very much. It's not helping anyone. The module description should be used to ... guess what ... describe the module.

}

/**
* The serialize visitor visits a FuncLiteralDeclaration
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$(REF dmd, func, FuncLiteralDeclaration) - in general there's also $(REF_ALTEXT myText, FuncLiteralDeclaration, dmd, func) if you prefer a custom text.

}

/**
* Entrypoint of the SerializeVisitor.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Params

override void visit(FuncLiteralDeclaration fld)
{
if (fld.type.ty == Terror)
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uncovered

value.writestring("arg");
value.print(i);
arg_hash.insert(key, strlen(key), value.extractString);
arg_hash.insert(&key[0], key.length, value.extractString);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
// Otherwise, the function is uncomparable
else
buf.reset();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW DStyle would require the else to use brackets here too.

{
v.getConstInitializer.accept(this);
}
// Otherwise, the function is uncomparable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Easy, use return. Then you only need to call buf.reset at the end.

import dmd.visitor;

/**
* This enum is used to report the type of the expression
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This enum is used to report can be excised with no change in meaning to the sentence.

/**
* This enum is used to report the type of the expression
* found when visiting the AST nodes to higher levels of the
* AST.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what visiting the AST nodes to the higher levels of the AST means.


/**
* The serialize visitor visits a FuncLiteralDeclaration
* AST node, serializing the implementation of the lambda
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does serialization of the implementation mean? Is a string produced? A hash?

@wilzbach wilzbach added this to the 2.079.0 milestone Feb 5, 2018
@wilzbach
Copy link
Contributor

wilzbach commented Feb 5, 2018

Does SerializeVisitor need to be exposed in a C++ header?

I don't see any reason why it should. It's more or less a "private function of the DMD frontend implemented as a visitor" for the other backends. They only care about the IR.

What happens if two lambdas are not comparable? Will compiler give an error or will you get back false in the comparison?

false - it's the __traits(isSame)
In fact at the moment the comparability is severely limited and returning false if unsure is a very reasonable decision.

I'm going to merge this now, s.t. it appears on the 2.079 changelog.
Great work again @RazvanN7!

@wilzbach wilzbach merged commit 3ad0b44 into dlang:master Feb 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants