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

constant folding affecting list/scalar context in conditional #14856

Open
p5pRT opened this issue Aug 16, 2015 · 1 comment
Open

constant folding affecting list/scalar context in conditional #14856

p5pRT opened this issue Aug 16, 2015 · 1 comment

Comments

@p5pRT
Copy link

p5pRT commented Aug 16, 2015

Migrated from rt.perl.org#125824 (status was 'new')

Searchable as RT125824$

@p5pRT
Copy link
Author

p5pRT commented Aug 16, 2015

From @karenetheridge

I don't totally understand what's going on, so I include the entire transcript here​:

14​:59 < ether> interesting, while the conditional operator is documented as propagating list context, and also can be used with lvalues, both cannot be done at the same time
14​:59 < ether> perl -wle'my @​list1 = (1,2); my @​list2 = (3,4); push (1 ? @​list1 : @​list2), 8; print @​list1; print @​list2;'
14​:59 < ether> Useless use of push with no values at -e line 1.
15​:00 < ether> huh, adding an extra set of parens around the push arguments seems to resolve this
15​:00 < Zefram> need moar parens!
15​:00 * ether breaks out B​::Deparse
15​:01 < Zefram> push(1?@​list1​:@​list2) , 8
15​:01 < Zefram> fix as push((1?@​list1​:@​list2), 8)
15​:01 < Zefram> or push +(1?@​list1​:@​list2), 8
15​:02 < Zefram> anyway, when corrected, the ?​: is propagating lvalueness there, but not list context
15​:03 < ether> in the former, why would the 8 not be captured as one of the push arguments?
15​:04 < Zefram> perl -lwe '@​l = (1,2); print \(@​l); print \(1?(@​l)​:(@​m))' # that's lvalue and list context
15​:04 < Zefram> because the paren as the next token after "push" means that push's whole argument list is parenthesised
15​:06 < ether> oh bah, of course.
15​:06 < ether> too many uses for parentheses :)
15​:06 < ether> I get so used to not using parens for builtins that I forgot that parens are also used for grouping arguments for sub calls :p
15​:06 < ether> push (1,2,3), 4; is of course taken as (push(1,2,3), 4)
15​:07 < Zefram> the paren doesn't make it a sub call
15​:07 < Zefram> the optionality of parenthesising the argument list is shared between builtins and subs
15​:08 < ether> *nod*
15​:08 < ether> but when they *are* there, that's the argument list passed to the sub/builtin, and nothing more, even if there are more terms that follow
15​:09 < Zefram> right
15​:09 < ether> the *very first* item on the list of operator precedence is "terms and list operators (leftward)" even :D
15​:14 < ether> now I'm stuck getting list context propagated when the LHS of the conditional is unambiguously a scalar, e.g. 1 == 2
15​:14 < ether> as that yields Experimental push on scalar is now forbidden
15​:14 < Zefram> what's your full statement?
15​:15 < ether> push( ( some-condition-here ? @​list1 : @​list2 ), $value );
15​:16 < Zefram> wfm
15​:16 * ether huhs. *fiddles*
15​:17 < ether> ok, strange - 1==2 works fine as a condition, but this does not​:
15​:17 < ether> perl -wle'my @​list1 = (1,2); my @​list2 = (3,4); push( ((ref(1)=="") ? @​list1 : @​list2), 8); print @​list1; print @​list2;'
15​:18 < ether> same with s/==/eq/
15​:18 < Zefram> right, I see a failure with that
15​:21 < Zefram> it depends on whether the condition gets constant-folded
15​:21 < Zefram> OK if it folds, error if not
15​:22 < ether> ohho!
15​:22 * ether works around the whole thing by using references :)
15​:22 < Zefram> and context *is* involved
15​:23 < Zefram> in the folding case, the rv2av ends up in list context. in the non-folding case it's in scalar context
15​:23 < Zefram> you should report it as a bug
15​:23 < Zefram> folding shouldn't make that difference
15​:24 < ether> ok
15​:24 < ether> do you mind if I copy this entire conversation into perlbug?
15​:25 < Zefram> perl -lXe 'push((1=="1a" ? @​a : @​b), 8); print @​a' works, but change -X to -w and it fails, because the warning inhibits constant folding
15​:25 < Zefram> go ahead

Perl Info
: [ether@jaeger ~/git]$; perl -V
Summary of my perl5 (revision 5 version 23 subversion 1) configuration:

Platform:
osname=darwin, osvers=14.4.0, archname=darwin-2level
uname='darwin jaeger 14.4.0 darwin kernel version 14.4.0: thu may 28 11:35:04 pdt 2015; root:xnu-2782.30.5~1release_x86_64 x86_64 '
config_args='-de -Dprefix=/Volumes/amaretto/Users/ether/perl5/perlbrew/perls/23.1 -Dman1dir=none -Dman3dir=none -Dusedevel -Aeval:scriptdir=/Volumes/amaretto/Users/ether/perl5/perlbrew/perls/23.1/bin'
hint=recommended, useposix=true, d_sigaction=define
useithreads=undef, usemultiplicity=undef
use64bitint=define, use64bitall=define, uselongdouble=undef
usemymalloc=n, bincompat5005=undef
Compiler:
cc='cc', ccflags ='-fno-common -DPERL_DARWIN -fno-strict-aliasing -pipe -fstack-protector-strong -I/opt/local/include',
optimize='-O3',
cppflags='-fno-common -DPERL_DARWIN -fno-strict-aliasing -pipe -fstack-protector-strong -I/opt/local/include'
ccversion='', gccversion='4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)', gccosandvers=''
intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678, doublekind=3
d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16, longdblkind=3
ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
alignbytes=8, prototype=define
Linker and Libraries:
ld='env MACOSX_DEPLOYMENT_TARGET=10.3 cc', ldflags =' -fstack-protector-strong -L/opt/local/lib'
libpth=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.1.0/lib /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib /usr/lib /opt/local/lib
libs=-lpthread -lgdbm -ldbm -ldl -lm -lutil -lc
perllibs=-lpthread -ldl -lm -lutil -lc
libc=, so=dylib, useshrplib=false, libperl=libperl.a
gnulibc_version=''
Dynamic Linking:
dlsrc=dl_dlopen.xs, dlext=bundle, d_dlsymun=undef, ccdlflags=' '
cccdlflags=' ', lddlflags=' -bundle -undefined dynamic_lookup -L/opt/local/lib -fstack-protector-strong'


Characteristics of this binary (from libperl):
Compile-time options: HAS_TIMES PERLIO_LAYERS PERL_COPY_ON_WRITE
PERL_DONT_CREATE_GVSV
PERL_HASH_FUNC_ONE_AT_A_TIME_HARD PERL_MALLOC_WRAP
PERL_PRESERVE_IVUV PERL_USE_DEVEL USE_64_BIT_ALL
USE_64_BIT_INT USE_LARGE_FILES USE_LOCALE
USE_LOCALE_COLLATE USE_LOCALE_CTYPE
USE_LOCALE_NUMERIC USE_LOCALE_TIME USE_PERLIO
USE_PERL_ATOF
Locally applied patches:
Devel::PatchPerl 1.34
Built under darwin
Compiled at Jul 21 2015 09:14:04
%ENV:
PERL5LIB="/Volumes/amaretto/Users/ether/.perlbrew/libs/23.1@std/lib/perl5"
PERLBREW_BASHRC_VERSION="0.73"
PERLBREW_HOME="/Volumes/amaretto/Users/ether/.perlbrew"
PERLBREW_LIB="std"
PERLBREW_MANPATH="/Volumes/amaretto/Users/ether/.perlbrew/libs/23.1@std/man:/Volumes/amaretto/Users/ether/perl5/perlbrew/perls/23.1/man"
PERLBREW_PATH="/Volumes/amaretto/Users/ether/.perlbrew/libs/23.1@std/bin:/Volumes/amaretto/Users/ether/perl5/perlbrew/bin:/Volumes/amaretto/Users/ether/perl5/perlbrew/perls/23.1/bin"
PERLBREW_PERL="23.1"
PERLBREW_ROOT="/Volumes/amaretto/Users/ether/perl5/perlbrew"
PERLBREW_VERSION="0.73"
PERLDOC_PAGER="less -sicMr"
PERL_AUTOINSTALL_PREFER_CPAN="1"
PERL_INSTALL_QUIET="1"
PERL_LOCAL_LIB_ROOT="/Volumes/amaretto/Users/ether/.perlbrew/libs/23.1@std"
PERL_MB_OPT="--install_base /Volumes/amaretto/Users/ether/.perlbrew/libs/23.1@std"
PERL_MM_OPT="INSTALL_BASE=/Volumes/amaretto/Users/ether/.perlbrew/libs/23.1@std"
@INC:
/Volumes/amaretto/Users/ether/.perlbrew/libs/23.1@std/lib/perl5/darwin-2level
/Volumes/amaretto/Users/ether/.perlbrew/libs/23.1@std/lib/perl5
/Volumes/amaretto/Users/ether/perl5/perlbrew/perls/23.1/lib/site_perl/5.23.1/darwin-2level
/Volumes/amaretto/Users/ether/perl5/perlbrew/perls/23.1/lib/site_perl/5.23.1
/Volumes/amaretto/Users/ether/perl5/perlbrew/perls/23.1/lib/5.23.1/darwin-2level
/Volumes/amaretto/Users/ether/perl5/perlbrew/perls/23.1/lib/5.23.1

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

No branches or pull requests

2 participants