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

Implement string.concat() and bytes.concat() #1590

Merged
merged 3 commits into from
Nov 9, 2023

Conversation

seanyoung
Copy link
Contributor

This is what solc implements. Solang has implement a + b for string concatenation, which this PR removes.

Fixes: #1558

@seanyoung seanyoung force-pushed the string-concat branch 3 times, most recently from 04ced55 to d7aee75 Compare November 7, 2023 14:49
Copy link

codecov bot commented Nov 7, 2023

Codecov Report

Merging #1590 (5a701d9) into main (e4b9a47) will increase coverage by 0.08%.
The diff coverage is 99.23%.

@@            Coverage Diff             @@
##             main    #1590      +/-   ##
==========================================
+ Coverage   87.57%   87.65%   +0.08%     
==========================================
  Files         133      133              
  Lines       64217    64300      +83     
==========================================
+ Hits        56236    56363     +127     
+ Misses       7981     7937      -44     
Files Coverage Δ
src/bin/languageserver/mod.rs 79.33% <ø> (+0.29%) ⬆️
src/codegen/cfg.rs 93.94% <ø> (-0.02%) ⬇️
src/codegen/events/polkadot.rs 100.00% <100.00%> (ø)
src/codegen/expression.rs 95.24% <ø> (-0.02%) ⬇️
src/codegen/mod.rs 83.72% <100.00%> (-0.16%) ⬇️
...expression_elimination/available_expression_set.rs 99.75% <100.00%> (-0.01%) ⬇️
...rc/codegen/subexpression_elimination/expression.rs 98.15% <ø> (+0.60%) ⬆️
src/codegen/subexpression_elimination/operator.rs 96.29% <ø> (-0.07%) ⬇️
src/emit/expression.rs 98.88% <100.00%> (+0.03%) ⬆️
src/sema/ast.rs 87.07% <100.00%> (-0.04%) ⬇️
... and 6 more

@LucasSte
Copy link
Contributor

LucasSte commented Nov 8, 2023

This is what solc implements. Solang has implement a + b for string concatenation, which this PR removes.

Fixes: #1558

Removing a+b is a breaking change and renders existing contracts incompatible. Shouldn't we first warn that a+b is deprecated and then remove it in a later release?

src/codegen/events/polkadot.rs Show resolved Hide resolved
stdlib/stdlib.c Show resolved Hide resolved
tests/polkadot_tests/strings.rs Show resolved Hide resolved
@seanyoung
Copy link
Contributor Author

Removing a+b is a breaking change and renders existing contracts incompatible. Shouldn't we first warn that a+b is deprecated and then remove it in a later release?

a+b for strings was always a non-standard extension. It is not used much; for example, in the solana program examples it is not used (I've also just tested that).

As such I don't think there is need for deprecation and eventual removal. I've added an entry to CHANGELOG.md.

Copy link
Contributor

@xermicus xermicus left a comment

Choose a reason for hiding this comment

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

LGTM, I just see some potential to optimize the concatenation into a no-op when one operand is an empty constant:

function f(string a) public returns (string) {
    return string.concat(a, "");
}
block0: # entry
        ty:string %a = (arg #0)
        return (builtin Concat ((arg #0), (alloc string uint32 0 "")))

@seanyoung
Copy link
Contributor Author

LGTM, I just see some potential to optimize the concatenation into a no-op when one operand is an empty constant:

function f(string a) public returns (string) {
    return string.concat(a, "");
}
block0: # entry
        ty:string %a = (arg #0)
        return (builtin Concat ((arg #0), (alloc string uint32 0 "")))

Nice, good catch. I've fixed this and added a test case.

This is what solc implements. Solang has implement a + b for string
concatenation, which this PR removes.

Fixes: hyperledger-solang#1558

Signed-off-by: Sean Young <sean@mess.org>
@LucasSte
Copy link
Contributor

LucasSte commented Nov 9, 2023

Removing a+b is a breaking change and renders existing contracts incompatible. Shouldn't we first warn that a+b is deprecated and then remove it in a later release?

It is not used much;

We don't know if it is not used much. There is no way to prove it. The solana examples is a small set of what people use Solang for. Polkadot also support this feature.

Instead of deprecating then, what do you think of suggesting people to use string.concat when erroring for string + string?

src/codegen/constant_folding.rs Outdated Show resolved Hide resolved
seanyoung and others added 2 commits November 9, 2023 14:43
Co-authored-by: Lucas Steuernagel <38472950+LucasSte@users.noreply.github.com>
Signed-off-by: Sean Young <sean@mess.org>
Signed-off-by: Sean Young <sean@mess.org>
@seanyoung
Copy link
Contributor Author

Instead of deprecating then, what do you think of suggesting people to use string.concat when erroring for string + string?

I've added a diagnostic for this. I think it's useful in general for folks when they may try to concatenate two strings.

@seanyoung seanyoung merged commit 45f01b4 into hyperledger-solang:main Nov 9, 2023
20 checks passed
@seanyoung seanyoung deleted the string-concat branch November 9, 2023 18:28
fanyi-zhao pushed a commit to fanyi-zhao/solang that referenced this pull request Nov 17, 2023
This is what solc implements. Solang has implement a + b for string
concatenation, which this PR removes.

Fixes: hyperledger-solang#1558

Signed-off-by: Sean Young <sean@mess.org>
Co-authored-by: Lucas Steuernagel <38472950+LucasSte@users.noreply.github.com>
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.

string.concat fails during sema
3 participants